Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there an easy way to find a child element from another element? We have a set of containers which hold many modules, and I'd like to ensure that they are displaying in their proper locations.

The API only seems to have the following method:

webElement.findElement(s).(By by);

Is there an easy way to do the following:

webElement.findElement(s)(WebElement webElement);
share|improve this question
I do not understand your question. What information would you expect to provide to describe the child element? Or are you asking how to determine whether an element is a child of another element? – user246 Apr 4 '12 at 15:05
I already have a WebElement which I've located (a module), I want to ensure that it's "in" another WebElement (i.e. a child). – Scott Apr 4 '12 at 15:15
WebElement's do not keep thier locator information once created and have no concept of parent's or children (This is why you occasionaly get StaleElementException's when navigating back and forth). You would have to extend the WebElement class to add in a reference to a parent element to acheive this. – Ardesco Apr 6 '12 at 10:05
@Ardesco there is no need to extends WebElement to do this as it turns out. You can simply extend the LocatorFactory. Since I have a new annotation it made it quite easy to have a two pass location of elements occur (when the factory returns null, location is not performed). – Scott Apr 24 '12 at 14:22

You can find only children of an element like this:

IWebElement parent = FindElement(...); //However you want to find the element

IWebElement childDivs = parent.FindElements(By.XPath("div"));

This would find you all child div's of that parent element, you can adjust the xpath to get you whatever you want.

If you already have the child and you want to find the parent you can do something similar:

IWebElement child = FindElement(...); //However you want to find the element

IWebElement parent = child.FindElements(By.XPath(".."));

You could then do whatever validation that you wanted to ensure that the parent is the element you were expecting.

share|improve this answer
Better answer than mine. – user246 Apr 4 '12 at 23:01
You can also use other By methods such as ClassName, CssSelector, etc. (not only XPath) to locate the child or parent element. – SteveCZ Apr 11 '12 at 14:25
a C# answer for a Java question but a good one – Franz Ebner Jun 29 '12 at 11:52

I do not know of a WebDriver API that will determine whether an element is a child of another. Here is an option: determine the XPath of both elements, then compare the XPaths to determine parentage.

You can use Javascript to determine an element's XPath. See for example You can use WebDriver's JavascriptExecutor interface to execute the Javascript.

I do not have a handy function to compare XPaths, but perhaps you can figure out that part on your own.

share|improve this answer
up vote 0 down vote accepted

With it being said that the annotation is basically being discarded once located, I'm just going to create my own ElementLocatorFactory, which is similar to the DefaultElementLocatorFactory. Since a SearchContext is needed to create the locator (which WebElement implements), I basically force the parents to be located first, then look for my child elements and catching exceptions where needed.

I was just hoping I had missed something in the core API that would allow for this functionality. I've also added a feature request in the google code repository.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.