My requirement is when I click on Facebook it should open the Facebook homepage. The xpath which I generated is //*[#class='sub2']/tbody/tr[2]/td/a
but still it is giving me NoSuchElementException. Please help me out in generating the correct xPath.
<div>
<center>
<table width="100%" class="sub2" style="float: none" border='8'
cellspacing="8" cellpadding="8">
<tbody>
<tr>
<th>
<center>Sample Program</center>
</th>
</tr>
<tr>
<td>
<a href="https://facebook.com">
<center> Facebook </center>
</a>
</td>
</tr>
<tr> </tr>
</tbody>
</table>
</center>
You can use XPath's normalize-space() as in //a[normalize-space()="Facebook"]
You can fetch the element by using the xpath:
//a[#href='https://facebook.com']
To locate the element with text as Facebook you can use either of the following Locator Strategies:
Using CssSelector:
"table td>a[href*='facebook']>center"
Using Xpath:
"//table//td/a/center[normalize-space()='Facebook']"
Reference
You can find a couple of detailed discussions on NoSuchElementException in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
NoSuchElementException, Selenium unable to locate element
It seems that you have messed up absolute xpath with relative xpath. if you start by using // it means you are using relative xpath. // means it can search the element anywhere at the webpage. Since you are trying to find a tag with the text 'Facebook' you can try //a[contains(text(),"Facebook")] which means select an anchor tag within the whole DOM which contains the text "Facebook".
Or you can try //*[contains(text(),"Facebook")] if you are quite sure there are no other elements containing the text 'Facebook', since the above xpath means "select all elemnts in the DOM where it contains the text 'Facebook' ".
Related
I am attempting to create a Robot Framework Selenium test where I input text into a Kendo grid filter and then check the results. The grid is here:
The filter code is here:
<thead class="k-grid-header" xpath="1">
<tr>...</tr>
<tr>
<td class="rowFilterContainer"></td>
<td class="rowFilterContainer"></td>
<td class="rowFilterContainer" data-field="UserId"></td>
<td class="rowFilterContainer" data-field="CandidateId">
<input class="rowFilter" data-field="CanddateId" data-type="string" type="text" data-
operator="startswith">
</td>
I am attempting to locate and then enter text in the CandidateId field using Selenium with an xpath of "//div[#id='Tab_0']//td[4]//input[1]" but when I run the test if complains that it cannot find it.
Any help on how to find this field (with a better xpath) and to add the text would be greatly appreciated. I have tried using CSS as well.
Try this xpath:
//td[#data-field='CandidateId']/input
Try this:
(.//thead[#class='k-grid-header']/tr)[2]/td[4]/input[#class='rowFilter']
Please see the image below which shows code of the web page in Developer Tools window and advise me the correct selector for 'Depreciation Models'. Text highlighted in red is constant, however the surrounding text is dynamic. So I tried using contains selector to locate but unsuccessful. I want to avoid XPATH as number of before and after div elements may keep changing.
I am using Selenium IDE hence the C#/Java code for RC/Webdriver won't help much.
Selenium IDE generated target path is this:
css=#dhxId_rgWATog7lC3E_27572|6059|6152 > td.sub_item_text > div.sub_item_text
I tried
css=contains('27572') > td.sub_item_text > div.sub_item_text
but it didn't work.
Kindly suggest. I am stuck. Thanks.
As you tried the Locator Strategy as :
css=contains('27572') > td.sub_item_text > div.sub_item_text
Reasons for not working
From the snapshot of the HTML you have provided, 27572 is not the innerText but partial string of the id attribute.
As per selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”:
The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).
Solution
You can use the following xpath as per the existing DOM Tree:
xpath=//tr[#class='sub_item'][contains(#id,'27572')]//td[#class='sub_item_text']/div[#class='sub_item_text'][contains(.,'Depreciations Models')]
I guess this would be the right approach:
<style>
[id*="27572"] > td.sub_item_text > div.sub_item_text{
color:red;
}
<table>
<tbody>
<tr id="dhxId_rgWATog7lC3E_27572|6059|6152" class="sub_item">
<td class="sub_item_icon">
<i class="fa fa-user epc-down-circled-2"></i>
</td>
<td class="sub_item_text">
<div class="sub_item_text"> Depriciations Models</div>
</td>
</tr>
</tbody>
</table>
It will set all elements that have an id attribute value containing "27572" and inside of it.
I am finding it hard to create a relative xpath for a check box . I have attached the code snippet below. Firebug gives me an absolute xpath as:
html/body/div[6]/div[2]/form/table[1]/tbody/tr[3]/td[1]/input[1].
But the form changes if there is separate entry and then absolute xpath changes to:
html/body/div[6]/div[2]/form/table[1]/tbody/tr[4]/td[1]/input[1]
(or tr[5] or any number). I tried to use the relative xpath but the firebug shows the xpath in red colour (so it is invalid)-
.//*[#id='responderSelection'155939'1']
Please help.
<form id="pollGroupForm" method="post" action="/consoleapp/newSurvey.jspx">
<div class="filter"/>
<table class="dataTable">
<tbody>
<tr>
<tr class="alternateRow">
<tr>
<td>
<input id="responderSelection'155939'1" type="checkbox" value="true" name="responderSelection['155939']"/>
<input type="hidden" value="on" name="_responderSelection['155939']"/>
</td>
<td>twoway_2</td>
<td> 26161 </td>
<td>
</tr>
</tbody>
</table>
<table>
</form>
</div>
</div>
<div id="footer">
Would you try a partial search using cssSelector? I assume the numbers with the id is dynamic so the following cssSelector let's you find the checkbox with the partial match of the id(^ means starts with)
[id^='responderSelection']
If you really want xpath then contains() is the equivalence to the css above
//input[contains(#id,'responderSelection')]
There are several different ways to do this, depending on the exact conditions.
If there is only on checkbox in that table, then you can focus on that: //table//input[#type='checkbox'].
Possibly safer selector would be to look for a checkbox in your form, as that has a fixed id attribute: //form[#id='pollGroupForm']//input[#type='checkbox']. If you can, always start from fixed ids.
If you want to get the Xpath of an element which ID is changing dynamically, there is a little trick: simply inspect the element with Firebug, delete its ID by double clicking on it (delete the whole id parameter, or change the value to "")and you will get the correct Xpath.
For example if you have the following structure:
<div id="fix-id">
<div id="dynamic-id-1214151"></div>
</div>
You will get //*[#id="fix-id"]/div[1] instead of //*[#id="dynamic-id-1214151"].
What is the code to be used to click the "a" element by looking first for the "td" that contains certain texts?
<table>
<tbody>
<tr>
<td><a class="link">link</a></td>
<td>1st</td>
</tr>
<tr>
<td><a class="link">link</a></td>
<td>2nd</td>
</tr>
</tbody>
</table>
I used this code but it doesn't work.
driver.findElement(By.xpath("//td[contains(text(), '1st')]/following-sibling::a[#class='link']")).click();
Try this xPath //td[contains(text(), '1st')]/../td/a[#class='link'].
Your mistake comes from the axis "following-sibling". The tag a is not a sibling of the td tag. I use .. to go up, then select td again and then a.
I tested the xPath using http://www.xpathtester.com/xpath. It works. Beware that every browser comes with their own xPath implementation, therefore it could behave a bit differently.
However, as there is nothing exotic in the xPath expression, I don't expect any problem.
I have the following html page source, and I tried to click the image by xpath/cssselector. none can work, could you help me to find the issue with my code? I use IE9.
<pretable border="0" cellpadding="0" cellspacing="0" width="700">
<tr>
<td rowspan="2" width="120">
<a href="#" onclick="oCMenu.m['top1'].b.moveIt(8,60); oCMenu.showsub('top1'); "
onclick="return false" class="FontNormal">
<img border="0" src="images/shim.gif" width="112" height="73"></a></td>
</tr>
</pretable>
my code is:
ieDriver.findElement(By.xpath("//html/table/tr[1]/td/a[#class='FontNormal']/img[#src='images
/shim.gif']")).click();
ieDriver.findElement(By.cssselector("class='FontNormal'")).click();
With the available DOM Structure we can always go with CSS Selector.
CSS Selectors
css=a[href='images/shim.gif']
css=a[href*='shim.gif']
Then Perform
driver.findElement(By.cssSelector("a[href='images/shim.gif']")).click();
OR
driver.findElement(By.cssSelector("a[href*='shim.gif']")).click();
You are trying to provide a full xpath and yet it does not match up with your html provided.
'table' is not the same as 'pretable', and you don't need to supply the full path anyway. Instead just try this for your xpath:
XPath("//a[#class='FontNormal']")
Here, xpath will search for any link with the class 'FontNormal' attached. If there is only one, this will select your element. If there is more you may need to be more specific.
First write proper xpath to identify that webelement where you want to perform click operation
driver.findElement(By.Xpath("//img[# src='images/shim.gif']")).click();
element(By.xpath('//html/table/tr[1]/td/a[#class='FontNormal']/img[#src='images
/shim.gif']')).click();
Use Firebug with Firepath for Mozilla Firefox. This will automatically generate xpath for you.
Webelement element = driver.findElement(By.xpath("*xpath here*");
element.click();
click on image link by using xpath.
driver.findElement(By.xpath("//img[contains(#src,'/static/images/image_name.png')]")).click();
You can change the image path depending on your image source.