Ruby Nokogiri - XPATH using URL - ruby

I have this table:
<tr>
<td><b>Amount</b></td>
<td><b>Due Date</b></td>
<td"><b>Link</b></td>
</tr>
<tr>
<td>02/13/2012</td>
<td>$81.66</td>
<td><a onclick="javascript:window.open('/cso/displaypdfbill?selectedBillkey=449409587','_blank');" href="javascript: void(0);">View Bill</a></td>
</tr>
<tr>
<td>01/13/2012</td>
<td>$181.66</td>
<td><a onclick="javascript:window.open('/cso/displaypdfbill?selectedBillkey=543409587','_blank');" href="javascript: void(0);">View Bill</a></td>
</tr>
I am looping through the table and extracting the Bill key in each row. I removed the Billkey and stored it into a variable.
BillKey = 449409587
What I want is to get the <tr> where that BillKey is located:
So I should have:
2/13/2012 81.86 View Bill
I am having trouble writing the XPATH to get the <tr>.

Use:
string(table/tr
[td/a/#onclick
[substring
(.,
string-length()
- 21
)
=
$vEnding
]
]
)
where $vEnding must be substituted by the string: "=449409587','_blank');"
So, the complete XPath expression after this substitution is:
string(table/tr
[td/a/#onclick
[substring
(.,
string-length()
- 21
)
=
"=449409587','_blank');"
]
]
)
XSLT - based verification:
This XSLT transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vEnding">=449409587','_blank');</xsl:variable>
<xsl:template match="/">
<xsl:copy-of select=
"string(table/tr
[td/a/#onclick
[substring
(.,
string-length()
- 21
)
=
$vEnding
]
]
)
"/>
</xsl:template>
</xsl:stylesheet>
when applied on the following XML document (the provided one wrapped in a single top element table):
<table>
<tr>
<td>
<b>Amount</b>
</td>
<td>
<b>Due Date</b>
</td>
<td>
<b>Link</b>
</td>
</tr>
<tr>
<td>02/13/2012</td>
<td>$81.66</td>
<td>
<a onclick=
"javascript:window.open('/cso/displaypdfbill?selectedBillkey=449409587','_blank');" href="javascript: void(0);">View Bill</a>
</td>
</tr>
<tr>
<td>01/13/2012</td>
<td>$181.66</td>
<td>
<a onclick=
"javascript:window.open('/cso/displaypdfbill?selectedBillkey=543409587','_blank');" href="javascript: void(0);">View Bill</a>
</td>
</tr>
</table>
evaluates the XPath expression and copies to the output the result of the evaluation:
02/13/2012
$81.66
View Bill

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 can I load this "label" using importxml & xPath

I've been trying to get this to load into google spreadsheet, but no success so far:
http://www.aastocks.com/tc/stocks/analysis/company-fundamental/basic-information?symbol=00027
The, I have the formula as follows:
=importxml ("http://www.aastocks.com/tc/stocks/analysis/company-fundamental/basic-information?symbol=" & To_Text(A7),"//td[#id='sb2-last']/label[#id='SQ_Last']/following-sibling::label/text()")
Result:
The content imported is empty.
The html part:
<table>
<tbody>
<tr>
<td>現價<label id="SQ_Currency">(港元)</label></td>
<td id="sb2-last">
<label id="SQ_Last" class="cls">**57.500**</label>
</td>
</tr>
</tbody>
</table>
try:
=INDEX(IMPORTXML(
"http://www.aastocks.com/tc/stocks/analysis/company-fundamental/basic-information?symbol="&
TO_TEXT(A7), "//td[#class='mcFont cls']"), 26)

XPATH for tr that has multiple text's in side it

I need XPATH for <tr> that contains text 'abc' in second <td> and text 'xyz' in third <td>
Tried but no luck.
final String XPATH = "//tr[td[contains(.,'abc')] and td[contains(.,'xyz')]";
Your expression actually almost selects what you want (once you fix the missing last ]). You just need to specify positions of the <td> elements.
//tr[td[2][contains(.,'abc')] and td[3][contains(.,'xyz')]]
For the following XML document:
<document>
<table>
<tr>
<td>foo</td>
<td>abc</td>
<td>xyz</td>
</tr>
<tr>
<td>foo</td>
<td>bar</td>
<td>xyz</td>
</tr>
</table>
</document>
this returns a node-set with the first <tr> element of the document in document order.

Not able to click on the nested anchor element using xpath of selenium webdriver

Please find the Html below :
<table class="data" id="filteredTable" cellpadding="0" cellspacing="1">
<tbody>
<tr class="rowLight">
<td class="lt4"><input name="ids" value="att1" type="checkbox"></td>
<td><a hfref= " link1" > foo </a>
</td>
<td>item1</td>
<td>item2</td>
<td>item3</td>
</tr>
<tr class="rowDark">
<td class="lt4"><input name="ids" value="att2" type="checkbox"></td>
<td><a hfref= " link2" > boo </a>
</td>
<td>item1</td>
<td>item2</td>
<td>item3</td>
</tr>
<tr class="rowLight">
<td class="lt4"><input name="ids" value="att3" type="checkbox"></td>
<td><a hfref= " link3" > bar </a>
</td>
<td>item1</td>
<td>item2</td>
<td>item3</td>
</tr>
Now I need to click on the link of bar. But my below Xpath not helping me to get into the bar as well. So any help how to be done the same.
I didn't give the html for the part //form[contains(#name,'filterset_FilterSetListForm')]/table[contains(#class,'contentBody')]/tbody/tr/td/table[contains(#class,'content')]/tbody/tr/td/. <~~ till this I am correct. Confusion start after <tr> from here /table[contains(#id,'filteredTable')]/tbody/tr
Part-II:
When there will be a match say bar , can their associated check box (s) be clicked?
Any help in this regard?
I am using selenium -web driver with Ruby 1.9.3 .
You can get the a element this way
/table[contains(#id,'filteredTable')]/tbody/tr/td/a[contains(text(),'bar')]
or if you want an exact match to the link text
/table[contains(#id,'filteredTable')]/tbody/tr/td/a[text()=' bar ']

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