How to use XPath to select following-sibling - xpath

<tr>
<td width="120" align="right" class="tit">application:</td>
<td style="width:345px;word-break:break-all;">
<a style="text-decoration: underline; color: #0066ff; cursor: pointer"
href="javascript:_search('pa', '<font color=red>PartA</font>PartB');"> <font color=red>PartA</font>PartB</a>;
</td>
</tr>
<tr>
In above code, I use part of the name (Part A) to search result, how can I get the whole name which combines PartA and PartB. I use below code and just get PartA and nothing
html.xpath('//td[contains(text(),"application")]/following-sibling::td[1]/a/font/text()')[0]
html.xpath('//td[contains(text(),"application")]/following-sibling::td[1]/a/text()')[0]

You need to fix your second XPath accordingly :
//td[contains(text(),"application")]/following-sibling::td[1]/a/text()[normalize-space()]
Output : Part B
To select the two items directly you can use :
//td[contains(text(),"application")]/following-sibling::td[1]//text()[normalize-space()]
To combine them :
concat(//td[contains(text(),"application")]/following-sibling::td[1]/a/font/text(),//td[contains(text(),"application")]/following-sibling::td[1]/a/text()[normalize-space()])
or
string(//td[contains(text(),"application")]/following-sibling::td[1]/a)
Output : PartAPartB

Related

XPath find text according last word in the string

I need to find the whole text according last word in the string. I have something like this:
<table>
<tr>
<td style='white-space:nowrap;'>
<a href=''>test</a>
</td>
<td>any text</td>
<td>text text texttofind</td>
<td>Not Available</td>
<td class='aui-lozenge aui-lozenge-default'>text</td>
</tr>
<tr>
<td style='white-space:nowrap;'>
<a href=''>test</a>
</td>
<td>any text</td>
<td>text text texttofind2</td>
<td>Not Available</td>
<td class='aui-lozenge aui-lozenge-default'>text</td>
</tr>
<tr>
<td style='white-space:nowrap;'>
<a href=''>test</a>
</td>
<td>any text</td>
<td>text text texttofind3</td>
<td>Not Available</td>
<td class='aui-lozenge aui-lozenge-default'>text</td>
</tr>
</table>
I need to find whole text vallue according last word texttofind
<td>text text texttofind</td>
I cant use contains, because it will find multiple values. I need something like ends-with but I am using xpath 1.0.
I tried something like this, but I am not sure what is wrong because it is not working
//tr[substring(., string-length(#td)
- string-length('texttofind') + 1) = 'texttofind']
or maybe it would be better to use matches?
You're almost there; try changing your xpath expression to
//tr//td[substring(., string-length(.)
- string-length('texttofind') + 1) = 'texttofind']
and see if it works.

how to find the root node on the basis of text of two attributes

I have a dynamic web table and I want to select the node on the basis of text value of two different text attributes.
//tr[.//td[contains(text(),'SATWIK GHANSIYAL')] and .//td[contains(text(),'07/07/2002')]]
HTML:
<html><head></head><body><table>
<tbody><tr style="background-color:White;height:24px;">
<td class="gridtext" align="center">
<span class="checkboxclass"><input id="ctl00_ContentPlaceHolder1_grdUsers_ctl02_chkSelect" type="checkbox" name="ctl00$ContentPlaceHolder1$grdUsers$ctl02$chkSelect" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$grdUsers$ctl02$chkSelect\',\'\')',
0)"></span>
</td><td class="gridtext" align="left" style="background-color:#FDE9D9;">SATWIK GHANSIYAL</td>
<td class="gridtext" align="left" style="background-color:#FDE9D9;" xpath="1">RAJESH GHANSIYAL</td>
<td class="gridtext" align="left" style="background-color:#FDE9D9;">SHELLY</td>
<td class="gridtext" align="left" style="background-color:#FDE9D9;">07/07/2002</td>
</tr>
</tbody></table>
</body></html>
I am getting the massage no element found
use this.
//tr[.//td[contains(.,'SATWIK GHANSIYAL')] and .//td[contains(.,'07/07/2002')]]
trying to evaluate this expression based on the given xml
//tr[.//td[contains(text(),'SATWIK GHANSIYAL')] and .//td[contains(text(),'07/07/2002')]]
text() is returning multiple sequence, thus giving me this error message.
Unable to perform XPath operation. A sequence of more than one item is not allowed as the first argument of contains() ("", "")

How to XPATH for Table element which have dynamic Ids

I have to get the XPATH for dynamic Ids . The code look like this
<table width="100%" role="presentation">
<tbody>
<tr>
<tr>
<td id="DWT10" role="presentation">
<div id="zl__TV-main__rows" class="DwtListView-Rows" style="height: 130px; width: 377px;">
<div id="zli__TV-main__654" class="RowDouble RowEven " role="treeitem" tabindex="0" aria-label="Unread, hello everyone, Wilkerson, 12:26 AM" aria-posinset="1" aria-level="1">
My XPATH goes like this :
//div[starts-with(#id='zl__CLV-main') and ./div [contains(#aria-posinset,'1')]]
I am getting:
Could not evaluate XPATH error.
XPAth only works if you have a valid XML source file. In your example there are no closing tags.
Your XPATH is invalid. the start-with function must be changed to
//div[starts-with(#id,'zl__CLV-main')
comma instead of =

XPath expression that matches the bgcolor attribute of a TR element

Working with HtmlAgilityPack against table rows that have been generated without name or id. Instead, i need to select based on the value contained in the row's bgcolor attribute:
I understand that XPath will return all rows where the name attribute = display:
foreach(HtmlNode cell in doc.DocumentElement.SelectNodes("//tr[#name='display']/td")
Given the code snippet below, what expression will select all elements when the row's bgcolor ="#FFFFFF">?
I've tried: SelectNodes(//tr[#bgcolor='#FFFFFF']/td")
> <tr bgcolor="#EAF2FA">
> <td colspan="2">
> <font style="font-family: sans-serif; font-size:12px;"><strong>Name</strong></font>
> </td> </tr> <tr bgcolor="#FFFFFF">
> <td width="20"> </td>
> <td>
> <font style="font-family: sans-serif; font-size:12px;">Steve</font>
> </td> </tr>
thx
bgcolor is weird, I find that using a contains will fix the problem.
This will work...
SelectNodes(//*//tr[contains(#bgcolor, 'FFFFFF')]/td")

getting value using xpath, ruby

I need to get value 9,70 from the following code, but am unable to do so. The number's comma is part of number and not delimiter, so the whole number is needed in one string. id="cheapest wine" is unique, but it keeps returning error.
<tr class="chartTableHeader">
<tr class="chartTableRow">
<td class="chartTableColFirst" style="height: 19px">
<td class="chartTableCol" style="height: 19px">
<td class="chartTableCol" style="height: 19px">
<span id="cheapest wine">9,70</span>
</td>
<td class="chartTableCol" style="height: 19px">
<td class="chartTableCol" style="height: 19px">
<td class="chartTableCol" style="height: 19px">
Using Nokogiri, and assuming that your html is formatted properly, you can get the value as follows:
require 'nokogiri'
xml = <<-EOF
<root>
<span id="cheapest wine">9,70</span>
</root>
EOF
doc = Nokogiri::XML(xml)
doc.xpath('//span[#id="cheapest wine"]').map do |add|
puts add.inner_text
end
Here the key is the XPath query: //span[#id="cheapest wine"] which searches for the span nodes whose id is "cheapest wine" (being an id, there should only be one).
Use the following XPath expression:
number(
translate(tr[#class='chartTableRow']/td/span[#id='cheapest wine'],
',',
'.'
)
)
where the current node from which the XPath expression is evaluated is the parent of the XML fragment shown in your question.
The XPath expression above evaluates to 9.7

Resources