tags:

views:

38

answers:

3

Hello!

I'm encoutered a problem that is could not get a HTML element by element's text.My HTML looks like:

...
<table>
  ...
  <tr>
    ...
    <td class="oMain">test value</td>
    ...
  <tr>
  ...
</table>
...

For some special reasons,I have to get the '<td class="oMain">' element using it's text 'test value'. I tried '//tr[td='test value']/td' but no result.How can i write the XPath expression?

Any help is welcome.Thanks!

A: 

What are you using to do the parsing? In Ruby + Hpricot, you can do

doc.search("//td.oMain").each do |cell|
  if cell.inner_html == "test value"
    return cell
  end
end

In this case, cell would be:

<td class="oMain">test value</td>
jekozyra
A: 

Instead of writing XPath you can alow use jQuery for getting the value like following

$(".oMain").html();

if you have multiple td in that case you can use

 $(".txt").each(function() {
//Your JS code here to fetch the value using this.html()

 });
Prakash Kalakoti
A: 

Your expression

//tr[td='test value']/td

places the predicate on the parent node "tr". Maybe that's what's causing the problem.

What you want probably is this

//td[@class = "oMain" and child::text() = 'test value']]

Here's a link to th W3 specification of the xPath language for further reading: http://www.w3.org/TR/xpath/

FK82