how to create xpath for the following check box - selenium

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"].

Related

how to find xpath of the input box in the following HTML

I have a html like this
<td class="random">
<div id="randomID">Product:</div>
</td>
<td class="random">
<div id="randomID">
<input type="text" class="random">
</div>
</td>
my xpath //div[contains(text(),"Product:")] gives me the first element for which I want to send input. How do I get the input xpath so I can do input.sendkeys() on it.
You use either of the xpath to get the input tag.
Use following and index of input tag
//div[contains(text(),"Product:")]/following::input[1]
Or find the td tag and then use following-sibling
//td[.//div[contains(text(),"Product:")]]/following-sibling::td[1]//input
You can use below xpath as well.
//div[text()="Product:"]/following::input[1]
Or
//td[.//div[text()="Product:"]]/following-sibling::td[1]//input
Use following-sibling
Try with following xpath:
//td[contains(.,"Product:")]//following-sibling::td//input
For the provided code, the input is very simple to identify:
//input[#class='random']
Since I can't see any other duplicate code to force you to be more precise, I don't see the problem in using this one.
If you have multiple , and each or some of them have an input inside, in this case:
//div[text()="Product:"]/following::input[1]
Where Product is the visible text of the div that you want to use as a parent, and then you go straight in the first input taken by its index.

Trouble generating the right Xpath

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' ".

Unable to find correct Target selector to click an element

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.

Clicking on checkbox based on span value in WebDriver

I Would like to select the the checkbox based on "Costco Wholesale Corporation" value in:
<td role="gridcell" style="" title="" aria-describedby="entity-search-grid_selected">
<input type="checkbox" name="chkbox" class="itmchk" value="110504">
</td>
<td role="gridcell" style="font-weight: bold;" title="Costco Wholesale Corporation" aria-describedby="entity-search-grid_name">
<div class="tree-wrap tree-wrap-ltr" style="width:18px;">
<div style="left:0px;" class="ui-icon ui-icon-triangle-1-s tree-minus treeclick"></div>
</div>
<span class="cell-wrapper">Costco Wholesale Corporation</span>
</td>
I have tried the following code:
driver.findElement(By.xpath("//td[span[text()='Costco Wholesale Corporation']]/preceding-sibling::td/input[#name='chkbox'"));
but getting following exception:
invalid selector: Unable to locate an element with the xpath expression //td[span[text()='Costco Wholesale Corporation']]/preceding-sibling::td/input[#name='chkbox' because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//td[span[text()='Costco Wholesale Corporation']]/preceding-sibling::td/input[#name='chkbox''
Could you help to write the xpath selector.
Your first problem is that xpath td[span[text() is definitely invalid. Remember that the [...] syntax is only for filters, not for defining the path. So it has to be:
//td/span[text...
Also since you select span first, you need to get back to td level and then choose a sibling:
//td/span[text()='Costco Wholesale Corporation']/../preceding-sibling::td/input[#name='chkbox']
Given your HTML, you could also simplify it to select td byt title and then its sibling:
//td[#title='Costco Wholesale Corporation']/preceding-sibling::td/input[#name='chkbox']
I wonder why you want to use xpath. The moment someone changes the structure of the site the whole selector will be worthless. I really recommend sticking to css selectors as long as you can. In this case it's just:
driver.findElement(By.css("input[name='chkbox'][value='110504']"));
And the main advantage of this approach is that only changes which directly concern the element will force you to change the selector.
Of course my selector might not be valid if there are some parts of site that you didn't show us and that may have inpact on selector. But even then I believe there is a way to use css to get to the element more directly.
Your provided xPath is syntactically incorrect.. try using below xPath :-
String xPath = "//span[text()='Costco Wholesale Corporation']/preceding::input[#name='chkbox']";
driver.findElement(By.xpath(xPath));
Hope it helps...:)

How can I insert a HTML tag before its parent tag using the Transformer from Genshi?

I need to modify the file table in my trac browser view by creating a class which implements the ITemplateStreamfilter class. I tried using the Transformer from genshi.filters.transform. My table looks like
<tbody>
<tr class="even">
<td class="name">
<a class="partent" title="Parent Directory" ..>..</a>
</td>
..
</tr>
..
</tbody>
I now need to insert a </td> tag just before the frist cell in the first row of the table. The problem is that I only can identify the position of column where I want to put the new cell befor by searching for the "Parent Directory" title: Transformer('//*[#title="Parent Directory"]'). How can I step one tag up than put the new cell before the first <td class="name"> tag?
I'm not THAT familiar with the support for XPATH of Transformer BUT:
What about
Transformer('(//td[*[#title="Parent Directory"]])[1]') and then using the before method?
As far as I understand, this should select the first td node with a child node with an attribute title="Parent Directory".
If you want to select any td with that kind of child node use
Transformer('//td[*[#title="Parent Directory"]]')
However, this only works if Transformer supports those XPATH expressions.
Edit 1
If you're sure, your td has an attribute class="name" you can also use Transformer('(//td[class="name" and *[#title="Parent Directory"]])[1]')