I am trying to traverse to the ancestor node from a child node using xpath, but it does not want to work.
WebElement menuItemText = driver.findElement(By.xpath("//span[contains( text(), '" + itemTitle + "')]"));
menuItemText = menuItemText.findElement(By.xpath(".//parent::*[@attribute='onclick']"));
menuItemText.click();
The nodes is:
<div onclick="something()">
<div onclick="myFunction()">
<div>
<div>
<span>Text!</span>
</div>
</div>
</div>
</div>
However, the function name is very complex and each node has strange unique IDs. Not that it should matter too much but I want to traverse up the DOM from the child node to the parent / ancestor node that includes an onclick
attribute. I want to get the first onclick when traversing up the DOM. So, the onclick=myFunction()
is what I want to get.
Is that possible? I tried doing a while() loop and it worked for once but not anymore.