XPath :: Get Next Sibling

I have the following HTML structure: I am trying to create a reliable method to extract the second element of the digest color, since there will be many of these tags in 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 td element "Color Digest" which has a decoded value.

I wrote the following xpath, but instead of getting the second, I do not get 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 elements.

+57
html xpath siblings scraper
Jul 25 '12 at 19:33
source share
4 answers

You should look for a second tr that has a td equal to "Color Digest", then you need to look either at the next sister of the first td in tr, or at 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

+84
Jul 25 '12 at 19:43
source share

You can find the item list id 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 element.get (1)

+3
Dec 04 '17 at 7:25
source share

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

In Chrome (maybe Safari too) you can check an element and then right-click on the tag for which you want to get xpath, then you can copy xpath to select that element.

0
Jul 25 2018-12-12T00:
source share

Can anyone help me figure out the xpath followed by brother for this? :)

 <tbody id="search-results"> <tr> <td>Name</td> <td><a href="/some/random/text" Somedddme1 </a></td> <td>reg kood: 123456778</td> </tr> <tr> <td>Name</td> <td><a href="some/random/text" Somdame2 </a></td> <td>reg kood: 123456778</td> </tr> <tr> <td>Name</td> <td><a href="some/random/text" Somaaname3 </a></td> <td>reg kood: 123456778</td> </tr> <tr> <td>Name</td> <td><a href="some/random/text" Soddddme4 </a></td> <td>reg kood: 123456778</td> </tr> <tr> <td>Name</td> <td><a href="some/random/text" Soge5 </a></td> <td>reg kood: 123456778</td> </tr> <tr> <td>Name</td> <td><a href="some/random/text" Some-me6 </a></td> <td>reg kood: 123456778</td> </tr> <tr> and so on....... 
0
Feb 06 '19 at 14:22
source share



All Articles