Find an ancestor with a particular class. Selenium. WebDriver. Using XPath

2k Views Asked by At

I am trying to find an XPath query to get an ancestor of my element, which is a div with a class='row publication-detail teaser'.

What I am currently doing is the following :

element.findElements(By.xpath("//ancestor::div[@class='row publication-detail teaser']"));

The problem is, it is returning all the siblings of div[@class='row publication-detail teaser']. And I only need the one that's my elements ancestor.

What am I doing wrong?

1

There are 1 best solutions below

0
cruisepandey On

You are almost correct, but findElements will give you list of elements instead use findElement

try replacing your code like this :

element.findElement(By.xpath("your web element here from where you want to go to ancestor, then use ancestor like this //ancestor::div[@class='row publication-detail teaser']"));