Locating Web Elements by walking up the DOM

1 vote

This is one of the rare cases where we need use XPath, as parent selecting is not supported in CSS. We prepared a sample to visualize this case. Take a look on the following website: when to use XPath

As you can see the amount of unique identifiers is very limited. However, we can select the `edit` or `delete` button based on the value which is present in the first table-cell.

The following example will select the `edit` button of the third row.

//td[contains(text(), '678')]/../td/button[@class = 'edit']

As you can see, we first select the right table-cell based on the text it contains. Then we go one level up ( ../) in the HTML structure and select the right button based on the `class`.

 

Examine your knowledge and play the ‘locator game‘.

Please follow and like us:
Follow by Email
Facebook
Google+
http://www.seleniuminaction.com/locating-web-elements-by-walking-up-the-dom/
Twitter
LinkedIn
Locating Web Elements by index matching

Leave a Reply

Your email address will not be published. Required fields are marked *