Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath:: Get following Sibling

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.

like image 613
add-semi-colons Avatar asked Jul 25 '12 19:07

add-semi-colons


People also ask

How do you write XPath with following siblings?

Example #1xpath("//a[contains(text()," + "'Second Window. ')]/parent::div//following-sibling::div[@class='cca']//a")); Here, the Firefox driver is invoked after the WebDriver API class 'Firefox Driver' is instantiated. The WebDriver object is then used to call and load the URL under test.

What does :: double colon in sibling XPath represent?

A double colon :: is used to separate the axis specifier from the node test. This XPath expression is a relative location path which selects all 'office' element children of the context node.

What is sibling in XPath?

A Sibling in Selenium Webdriver is a function used to fetch a web element which is a sibling to the parent element. If the parent element is known then the web element can be easily found or located that can use the sibling attribute of the Xpath expression in selenium webdriver.


1 Answers

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

like image 103
james31rock Avatar answered Sep 28 '22 14:09

james31rock