XPath to find ancestor node containing CSS class

Posted by Juan Mendes on Stack Overflow See other posts from Stack Overflow or by Juan Mendes
Published on 2012-04-09T16:48:37Z Indexed on 2012/04/09 17:29 UTC
Read the original article Hit count: 439

Filed under:
|
|

I am writing some Selenium tests and I need to be able to find an ancestor of a WebElement that I have already found.

This is what I'm trying but is returning no results

// checkbox is also a WebElement
WebElement container = checkbox.findElement(By.xpath(
    "current()/ancestor-or-self::div[contains(@class, 'x-grid-view')]") );

The image below shows the div that I have selected highlighted in dark blue and the one I want to find with an arrow pointing at it.

enter image description here

UPDATE Tried prestomanifesto's suggestion and got the following error

[cucumber]       org.openqa.selenium.InvalidSelectorException: The given selector ./ancestor::div[contains(@class, 'x-grid-view']) is either invalid or does not result in a WebElement. The following error occurred:
[cucumber]       [InvalidSelectorError] Unable to locate an element with the xpath expression ./ancestor::div[contains(@class, 'x-grid-view']) because of the following error:
[cucumber]       [Exception... "The expression is not a legal expression."  code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)"  location: "file:///C:/Users/JUAN~1.MEN/AppData/Local/Temp/anonymous849245187842385828webdriver-profile/extensions/fxdriv

Update 2 Really weird, even by ID is not working

[cucumber]       org.openqa.selenium.NoSuchElementException: Unable to locate element:{"method":"xpath","selector":"./ancestor::div[@id='gridview-1252']"}

 

© Stack Overflow or respective owner

Related posts about java

Related posts about xpath