Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:56:36+00:00 2026-06-01T11:56:36+00:00

I am writing some Selenium tests and I need to be able to find

  • 0

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

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

 

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:56:37+00:00Added an answer on June 1, 2026 at 11:56 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some UI tests using Selenium and i have a JavaScript Tree
In writing some tests for a project I'm working on, I found that the
I'm writing a selenium test using the Play! Framework and some tests have common
I'm writing some Selenium tests in Java, and I'm mostly trying to use verifications
I'm writing some data acquisition software and need a gui plotting library that is
I'm writing some selenium tests (in rspec for a rails app). One of my
I'm writing some automated tests using Capybara/RSpec, I choose/configure the driver by using this
In writing some image processing routines in Matlab, I found that I don't know
I have some trouble writing some code (I might be tired), so I need
I am writing Selenium 2 code that searches Google by some criteria, then clicks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.