Skip to content Skip to sidebar Skip to footer

How To Get A Sub Parent Web Element Component Between Many Div In Selenium Xpath?

I want to get the sub parent element
given the text of a post in this case given the text 'post2' that is the second div in the code there are many posts

Solution 1:

a solution that exists is using the "ancestor" keyword:

//div[@id='content']//span[contains(text(), 'post2')]/ancestor::div[@class='post']

Solution 2:

Alternatively, you can put the 'span checking' as predicate for the <div class="post"> :

//div[@id='content']/div[@class='post'][.//span[contains(text(),'post2')]]

This way you don't need to go back up the tree to the ancestor <div class="post"> after going straight to the span

Post a Comment for "How To Get A Sub Parent Web Element Component Between Many Div In Selenium Xpath?"