3

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);
4
  • 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
    Commented Apr 4, 2012 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
    Commented Apr 4, 2012 at 15:15
  • 1
    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
    Commented Apr 6, 2012 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
    Commented Apr 24, 2012 at 14:22

3 Answers 3

6

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.

3
  • Better answer than mine.
    – user246
    Commented Apr 4, 2012 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
    Commented Apr 11, 2012 at 14:25
  • a C# answer for a Java question but a good one Commented Jun 29, 2012 at 11:52
1

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 http://snippets.dzone.com/posts/show/3754. 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.

1
  • That snippet url returns a 404. Commented Mar 8, 2021 at 23:11
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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