title: XPath to find ancestor node containing CSS class
body: <p>I am writing some Selenium tests and I need to be able to find an ancestor of a <code>WebElement</code> that I have already found.</p>
  
  <p>This is what I'm trying but is returning no results</p>
  
  <pre><code>// checkbox is also a WebElement
  WebElement container = checkbox.findElement(By.xpath(
      "current()/ancestor-or-self::div[contains(@class, 'x-grid-view')]") );
  </code></pre>
  
  <p>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.</p>
  
  <p><img src="https://i.stack.imgur.com/XQzS9.png" alt="enter image description here"></p>
  
  <p><strong>UPDATE</strong>
  Tried prestomanifesto's suggestion and got the following error</p>
  
  <pre><code>[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
  </code></pre>
  
  <p><strong>Update 2</strong>
  Really weird, even by ID is not working</p>
  
  <pre><code>[cucumber]       org.openqa.selenium.NoSuchElementException: Unable to locate element:{"method":"xpath","selector":"./ancestor::div[@id='gridview-1252']"}
  </code></pre>
  
  <p><strong>Update 3</strong> </p>
  
  <p>The following XPATH works, but is brittle</p>
  
  <pre><code>../../../../../../../*
  </code></pre>
  
  <p> </p>