Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath to find ancestor node containing CSS class

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']"}

Update 3

The following XPATH works, but is brittle

../../../../../../../*

 

like image 739
Juan Mendes Avatar asked Apr 09 '12 16:04

Juan Mendes


People also ask

What is ancestor XPath?

Definition of XPath Ancestor. The ancestor axis chooses all of the current node's ancestor elements (parent, grandparent, great-grandparents, and so on). The root node is always present on this axis (unless the current node is the root node).

How do I find the parent of an element in XPath?

A string of elements is normally separated by a slash in an XPath statement. You can pick the parent element by inserting two periods “..” where an element would typically be. The parent of the element to the left of the double period will be selected.

How do you write XPath for child nodes in selenium?

New Selenium IDE We can locate child nodes of web elements with Selenium webdriver. First of all we need to identify the parent element with help of any of the locators like id, class, name, xpath or css. Then we have to identify the children with the findElements(By. xpath()) method.

How do I traverse back in XPath?

IN the XPath we can traverse backward using the double dots(..). We can traverse backward as many levels as we want like ../../… For example, if we want to find the parent of any child div selector then we can use it as.


1 Answers

This should select the element you want

./ancestor::div[contains(concat(' ', @class, ' '), ' x-grid-view ')][1]

In plain English: Of all the ancestor div elements that have ' x-grid-view ' in their class, select the first (closest) one.

Notes:

  • I concat spaces as a defensive measure to prevent partial matches.
  • current() is an XSLT function, not an XPath one. It has no meaning outside of XSLT. The current node is expressed as . in XPath.
like image 156
Tomalak Avatar answered Sep 24 '22 02:09

Tomalak