86

I have following HTML Structure: I am trying to build a robust method to extract second color digest element since there will be many of these tag within the DOM.

<table>
  <tbody>
    <tr bgcolor="#AAAAAA">
    <tr>
    <tr>
    <tr>
    <tr>
      <td>Color Digest </td>
      <td>AgArAQICGQMVBBwTIRQHIwg0GUMURAZTBWQJcwV0AoEDAQ </td>
    </tr>
    <tr>
      <td>Color Digest </td>
      <td>2,43,2,25,21,28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, </td>
    </tr>
  </tbody>
</table>

I am trying to extract the Second "Color Digest" td element that has the decoded value.

I wrote the following xpath but instead of getting the second i am not getting the second td element.

//td[text() = ' Color Digest ']/following-sibling::td[2]

And when I change it to td[2] to td[1] I get both the elements.

Ripon Al Wasim
  • 34,088
  • 37
  • 146
  • 165
add-semi-colons
  • 14,928
  • 43
  • 126
  • 211

3 Answers3

126

You should be looking for the second tr that has the td that equals ' Color Digest ', then you need to look at either the following sibling of the first td in the tr, or the second td.

Try the following:

//tr[td='Color Digest'][2]/td/following-sibling::td[1]

or

//tr[td='Color Digest'][2]/td[2]

http://www.xpathtester.com/saved/76bb0bca-1896-43b7-8312-54f924a98a89

mlissner
  • 14,662
  • 15
  • 82
  • 149
james31rock
  • 2,345
  • 2
  • 17
  • 24
  • I am using Xpath Checker on firefox – add-semi-colons Jul 25 '12 at 19:47
  • i used this to test. http://www.xpathtester.com/saved/b452f721-556c-4e9b-9758-1910b0871d4a – james31rock Jul 25 '12 at 19:48
  • 1
    maybe the xpath checker doesnt work? I never used it and do not have firefox installed. The xpath I have should only return the second td element, when the first td element has 'Color Digest' – james31rock Jul 25 '12 at 19:51
  • If you check my HTML there is a Space " Color Digest " i tried the link you sent and you can see the both TD are getting returned. – add-semi-colons Jul 25 '12 at 19:54
  • 1
    Ahh I see, I'm going to change my response. Sorry. You should be looking for the second tr that has the td that equals ' Color Digest ', then you need to look at either the following sibling of the first td in the tr, or the second td. – james31rock Jul 25 '12 at 19:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14459/discussion-between-james31rock-and-null-hypothesis) – james31rock Jul 26 '12 at 13:13
5

You can go for identifying a list of elements with xPath:

//td[text() = ' Color Digest ']/following-sibling::td[1]

This will give you a list of two elements, than you can use the 2nd element as your intended one. For example:

List<WebElement> elements = driver.findElements(By.xpath("//td[text() = ' Color Digest ']/following-sibling::td[1]"))

Now, you can use the 2nd element as your intended element, which is elements.get(1)

2

/html/body/table/tbody/tr[9]/td[1]

In Chrome (possible Safari too) you can inspect an element, then right click on the tag you want to get the xpath for, then you can copy the xpath to select that element.

FarFigNewton
  • 6,600
  • 11
  • 50
  • 74
  • 2
    Yeahe I know that function there are repetition of these elements rather than just getting for one I want to build a robust approach. – add-semi-colons Jul 25 '12 at 19:41