How to click on an icon through xpath consistently using Selenium - c#

Please see below the xpath expression copied from the developer tools.
//*[#id="parentDiv"]/table/tbody/tr[1]/td[8]/div/profile-link-column/a/i
I want to click on the 8th table data in first row(actually its a small icon), so this works fine 3 out of 10 times.
Can somebody suggest a more reliable approach?
This is my HTML:
<td>
<!--anchor-->
<div class="animated-slide-in au-enter-active">
<profile-link-column device-id="${value.value}" class="au-target" au-target-id="395">
<a click.trigger="viewProfile()" class="au-target" au-target-id="55">
<i class="fa fa-newspaper-o"></i>
</a>
</profile-link-column>
</div>
<!--anchor-->
<!--anchor-->
<!--anchor-->
<!--anchor-->
<!--anchor-->
</td>

Use css here
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.cssSelector("i.fa.fa-newspaper-o")).click
check whether it works fine, if it doesn't work, then please paste the error, If it's related to visibility error, then I think the problem is, <profile-link-column device-id="${value.value}" class="au-target" au-target-id="395"> <a click.trigger="viewProfile()" class="au-target" au-target-id="55"> <i class="fa fa-newspaper-o"></i> </a> </profile-link-column> is hidden in your html.

To click on the desired element you have to induce WebDriverWait for the element to be clickable as follows :
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//td/div[#class='animated-slide-in au-enter-active']/profile-link-column[#class='au-target']/a[#class='au-target']/i[#class='fa fa-newspaper-o']"))).Click();

Related

Selenium not recognizing span element within a div, thinks it's text?

I'm trying to grab the text from a span that's inside a div. The div is currently selected, so it has "curr" within its class.
The DOM:
<a id="ctl00_oAjaxContentPlaceHolder_LinkButtonAlerts" href="javascript:__doPostBack('ctl00$oAjaxContentPlaceHolder$LinkButtonAlerts','')">
<div id="ctl00_oAjaxContentPlaceHolder_divAlertAlertsHolder" class="profile-menu-alerts curr" title="Activities & Alerts">
<span>Activities & Alerts</span>
</div>
</a>
This XPath should find the span (it works when I use the Find tool in DevTools), but it fails to find the element
//div[contains(#class,'curr')]/span
If I remove the /span from the xpath, it finds the div just fine. And the strange part is that if I grab the text of that div with
driver.FindElement(By.XPath("//div[contains(#class,'curr')]")).Text;
it returns "<span>Activities & Alerts</span>". Why is this span element being incorrectly recognized as Text?
I ran this on my solution using the below and had no issues.
var test = Driver.FindElement_byXPath("//div[contains(#class,'curr')]/span").Text;
html - added another option:
<a id="ctl00_oAjaxContentPlaceHolder_LinkButtonAlerts" href="javascript:__doPostBack('ctl00$oAjaxContentPlaceHolder$LinkButtonAlerts','')">
<div id="ctl00_oAjaxContentPlaceHolder_divAlertAlertsHolder" class="profile-menu-alerts" title="Activities & Alerts">
<span>Test 1</span>
</div>
</a>
<a id="ctl00_oAjaxContentPlaceHolder_LinkButtonAlerts" href="javascript:__doPostBack('ctl00$oAjaxContentPlaceHolder$LinkButtonAlerts','')">
<div id="ctl00_oAjaxContentPlaceHolder_divAlertAlertsHolder" class="profile-menu-alerts curr" title="Activities & Alerts">
<span>Activities & Alerts</span>
</div>
</a>

I cant Locate the Xpath of Pseudo Comment

I just want to know how to get the Xpath Of pseudo Element Actually the thing is that in This I want to .CLick() on this Div Its role Is button so i tried just To xpath with Div Class name it is locating the element but Not clicking i have to Click On this ::after But i cant Get the Xpath Because it is a Pseudo Element
<div id=":l6" class="T-I J-J5-Ji amD T-I-awG T-I-ax7 T-I-Js-Gs L3"
role="button" tabindex="0"
data-tooltip="Older"
aria-label="Older"
style="user-select: none;"
xpath="1">
<span class="amF"
aria-hidden="true">
</span>
<img class="amJ T-I-J3"
src="images/cleardot.gif" alt="">
::after
</div>
What I have read "The Selenium API doesn't support pseudo-elements".
So you can use try using Actions.
I have a sample (I don't have the full path you can add) here with CssSelector:
Actions action = new Actions(Session.Driver);
action.MoveToElement(FindElement(By.CssSelector("div.img::after"))).ContextClick().Build().Perform();
As a last resort, you can use JavaScript with ExecuteScript something like this:
var afterElem = driver.FindElement(By.CssSelector("div.img"));
var buttonAfter = driver.ExecuteScript(
"return window.getComputedStyle(arguments[0],'::after');"
, afterElem);
buttonAfter.click();
You will need to play with the CssSelector Or use an XPath.
Hope this helps you!

Selenium throws "OpenQA.Selenium.NoSuchElementException" in C# program

I'm programming Selenium using C#, but I don't understand how to use it.
For example, I want to check the XPath that I want to click from HTML.
This is the HTML I want.
<span class="option_text">10</span>
Below is the original HTML
<div class="report_options _options">
<a href="#" class="option" data-reportpolicy="R1">
<i class="ico as_radio"></i>
<span class="option_text">1</span>
</a>
<a href="#" class="option" data-reportpolicy="R2">
<i class="ico as_radio"></i>
<span class="option_text">2</span>
</a>
.
.(Omitted)
.
<a href="#" class="option" data-reportpolicy="R11">
<i class="ico as_radio"></i>
<span class="option_text">10</span>
</a>
</div>
I used
WebDriver.FindElement(By.XPath("//span[contains(#class,'option_text') and contains(text(), '10')]")).Click();
but I could not. How can I fix this?
PS. Visual Studio error log:
OpenQA.Selenium.NoSuchElementException: 'no
such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id='report-question-layer']/div[2]/div/div[3]/div/div[2]/div/a[10]"}
(Session info: chrome=60.0.3112.101) (Driver info:
chromedriver=2.31.488763
(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT
10.0.14393 x86_64)'
Use below XPATH :-
//a[#class='option' and #data-reportpolicy='R11']//span[#class='option_text']
OR
//*[#id="report-question-layer"]/div[2]/div/div[3]/div/div[2]/div/a[10]/span
OR
//div[#id='report-question-layer']//div[#class='report_options _options']//a[#data-reportpolicy='R11']/span
OR
//a[#data-reportpolicy='R11']/span
Hope it will help you :)
Checking in the HTML, You can try to use this way:
First, try to switch to the frame:
WebDriver.SwitchTo().Frame(driver.FindElement(By.Name("fb_xdm_frame_https")));
Then try the following XPath to click on it:
WebDriver.FindElement(By.XPath("//span[contains(text(), 'その他の禁止されている行為')]")).Click();

Selenium webdriver c# - unable to click() element (element not null)

I seem to be having issues clicking a element inside a box which is filled by ajax.
So the web page I am on has a link on it which when clicked this calls a javascript function which then inserts a new div into the page which is full of new content.
Now the weird thing is I can find the element inside this box no problem using xpath, and I can even read its value but! I can't use Click(); on the link inside the box the event just wont work for some reason.
Has anyone faced a similar issue and know a work around?
I am using Selenium webdriver 2.35 with Firefox 23
More Info
OK so the HTML for the link I click which calls the JS to make the div appear.
<center>
<a id="link_fleet_move_here" href="">Move fleet here</a>
</center>
<br>
<script>
$("#link_fleet_move_here").click( function(event) { event.preventDefault(); load_fleet_move_to_destination("fleet.aspx?method=ajax&view=move_to_destination&version=1&player=111&destination=LZLOCATION"); $("#link_fleet_move_here").hide();} )
</script>
<center>
<div id="fleetLoaderTemplate" style="display:none">
<div id="fleetLoaderErrorTemplate" style="display:none">
</center>
<div id="move_to_destination_container"></div>
And when the event finishes loading the new HTML
<div id="move_to_destination_container">
<ajax>
<table width="600" align="center">
BIG TABLE FULL OF CONTENT
<td sorttable_customkey="LZLOCATION">
(LZLOCATION)
</td>
<td sorttable_customkey=""></td>
<td sorttable_customkey=""></td>
<td>
Move
</td>
<table>
<br>
</ajax>
</div>
The Selector
location = driver.FindElement(By.XPath("//a[contains(#href, '" + LZLocation + "')]/following::td[3]"));
location.Click();
I think it may be something to do with that div actually, I think it starts as Display:None and gets changed, this will be effecting it?
I thought it was dynamically adding it but maybe not!
Try selecting your element via the following:
driver.findElement(By.cssSelector("#move_to_destination_container a[href^='fleet']")).click();
If it throws an error, try using:
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#move_to_destination_container a[href^='fleet']"))).click();

unable to click element with value zero

I am trying to click on the div tag that has id="0". Although it can find that id, unable to click on it. clicking on other ids work.
This was working in Firefox 12 but failing in 13
I am using Selenium2 with C#
driver.FindElement(By.XPath("//div[#class='MenuItem' and #id='0']")).Click();
<div style="position: absolute; top: 72px; left: 9px; " id="file" class="Menu">
<div class="MenuItem" id="0"> <img src="images/UI/Icons/folder.png">Folder</div>
<div class="MenuItem" id="1"> <img valign="middle" src="images/UI/Icons/tree/folderBlogClosed.png">Blog</div>
<div class="MenuItem" id="2"> <img valign="middle" src="images/UI/Icons/folderBoard.png">Discussion Board</div>
</div>
Elements with ID attributes that start with numbers are invalid HTML. While most browsers will render this just fine, it's entirely possible that attempting to use JavaScript to locate the element by the ID (as Selenium WebDriver does) will fail.
Is there a reason you're using an XPath and not just selecting by the ID?
driver.FindElement(By.Id("0"));
HTML id's should be unique on a page.
I resolved this issue by using mouse actions
. Thank you all for your input.
Actions builder = new Actions(driver);
IWebElement tagElement = driver.FindElement(By.XPath("//div[#id='0' and #class='MenuItem']"));
//mouse hover on first item
builder.MoveToElement(tagElement).Perform();
//click on first item
driver.FindElement(By.XPath("//div[#id='0' and #class='MenuItem']")).Click();

Categories

Resources