Xpath help to Find Unique value - html

I want to find the first tr tag with PONumber: text. I am not able to do that. Any help? I can find it using the //table/tbody/tr/td[contains(text(),'PONumber')] but it gives 2 objects. I want to find the first one only.
<tr>
<td class="clsLabel" align="right"> PONumber: </td>
<td class="clsInput"> PN659 </td>
</tr>
<tr>
<td class="clsLabel" align="right"> PreviousPONumber: </td>
<td class="clsInput"/>
</tr>

You can use following xpath to find exact object which you want
//tr/td[normalize-space(.)='PONumber:']

You can use something like
(//tr/td[contains(text(),'PONumber')])[1]
so put the xpath in brackets and with [1] you can specifiy to only return the first entry. Otherwise you could also use something like:
//tr/td[contains(text(),'PONumber') and not(contains(text(),'Previous'))]
so "Previous" will be excluded from the search results

You can limit the XPath result to return only the first matched by using [1] :
(//table/tbody/tr/td[contains(.,'PONumber')])[1]

Related

Xpath to select tr based on specific td not containing text

I'm trying to write a ruby/selenium script to click the first check box in a table where the row does not contain certain values (plural) for //tr/td[6].
Each tr in the table is structured as follows:
<tr>
<td>
<input name = "checkbox" type = "checkbox"></input>
</td>
<td> irrelevant </td>
<td> irrelevant </td>
<td> irrelevant </td>
<td> irrelevant </td>
<td> text I care about </td>
</tr>
I need the xpath for the checkbox of the first tr in the table where//tr/td[6] does not contain "badtext" and does not contain "badtext2"
Not exactly sure how to write an xpath for this. Hopefully I explained this well enough.
You need to use the not(expression) function like this
//tr/td[6][not(contains(text(),'badtext'))][not(contains(text(),'badtext2'))]
and your final xpath would be like this
//tr/td[6][not(contains(text(),'badtext'))][not(contains(text(),'badtext2'))]/../td/input[#name='checkbox']
if you want to filter any attribute including text, use
contains(.,'badtext')

xpath replace not working?

With this HTML
<tr>
<td class="listOddRow">Chequing </td>
<td class="listOddRow">00-227-03</td>
<td class="listOddRow" nowrap="">0275-1</td>
<td class="listOddRow" align="right" nowrap="">$ 28.08</td>
</tr>
Anyone knows why this works
//td[contains(text(),"00-227-03")]/parent::tr//a
but not this? I want to remove the dashes from the text() before calling contains()
//td[contains(replace(text(), "-", ""),"0022703")]/parent::tr//a
At least in xpath 1.0 there is no function replace, but there is translate working in the same way - it replaces in the 1st string chars presented in the 2nd one by corresponding chars in the 3rd. So, you can use the Xpath
//td[contains(translate(text(), "-", ""),"0022703")]/parent::tr//a

Xpath using sibblings or fellowing in two defrent Cell

Put bluntly I want to locate TestCoupon10% inside td then open a sibling td then locate //a[contains(#id,"cmdOpen")] I did try sibling and fellowing but likely I didnt do it right because
//span[./text()="TestCoupon10%"]/following-sibling:a[contains(#id,"cmdOpen")]
result into an invalid xpath. the HTML structure look as fellow
<tr>
<td>
<span id="oCouponGrid_ctl03_lblCode">TestCoupon10%</span>
</td>
<td>...</td>
<td>...</td>
<td valign="middle" align=""right">
<a id="oCouponGrid_ctl03_cmdOpen">
</td>
</tr>
I need to find cmdOpen and test coupon does anyone has an idea how to?
Axes are delimited with double colons, not single ones (those are used for namespace prefixes). You wanted to say this:
//span[./text()="TestCoupon10%"]/following-sibling::a[contains(#id,"cmdOpen")]
But - the <a> is not a following sibling of the <span> in question. You need to do some navigating:
//span[./text()="TestCoupon10%"]/parent::td/following-sibling::td/a[contains(#id,"cmdOpen")]
Or, simply avoid descending into the tree you you don't have to "climb up" again in the first place.
//td[span = "TestCoupon10%"]/following-sibling::td/a[contains(#id,"cmdOpen")]

Xpath to an element with two different contains text

I have the next HTML and i have this xpath to find the "Show":
xpath=//*[#id="Some_id"]/div/table/tbody/tr/td[contains(text (), "Show")]
and it works, but i need to find "Show" of a particular item, in this matter of a "Main" item, so i need smth like this:
xpath=//*[#id="Some_id"]/div/table/tbody/tr/td[contains(text (), "Show")]/preceding-sibling::td[contains(text (), "Main")]
but it doesn't work. Thanks
<tr class="even">
<td title="Main">
<strong>Main</strong>
</td>
<td>
text/html
</td>
<td>
Another text
</td>
<td>
Some text here
</td>
<td>
No
</td>
<td>
Show
</td>
</tr>
You can try this xpath. This will first select a tr element which contains the specific td and then select the required a tag.
"//tr[#class='even' and td[#title='Main']]/a[text()='Show']"
EDIT: This xpath worked for the OP
"//*[#id='Some_id']/div[1]/table/tbody/tr[#class='even']/td/a[contains(text (), 'Show')]"
You want to find "Show" that belong to the "Main" row in the table.
Here is what i would do
xpath=//td[#title='Main']/following-sibling::td[contains(text(), 'Show')]
More on xpath axes: http://www.w3schools.com/xsl/xpath_axes.asp

xpath ignore node

I'm looking for a way to select a node in xpath, giving that a node on it's path may exist or not. Just like '?' works in regexp ;)
For instance, I'd like to figure out a xpath query to get to <td> regardless of the case whether <tbody> node exists or not, with something like /table/(tbody)?/tr/td. I'd like it to work in both cases:
<table>
<tr>
<td />
</tr>
</table>
and
<table>
<tbody>
<tr>
<td />
</tr>
</tbody>
</table>
This may fail to cover more complex cases, but in this example using /table/tbody/tr/td | /table/tr/td should do the trick.
You can do:
//table/descendant::tr/td
or
//table//tr/td
depending on your taste. The double slash is a "look that up somewhere on this level or deeper" (more formally, descendant-or-self:: axis). The spec is, surprisingly, a very good read on this!