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);
Scott
  • 397
  • 4
  • 15
  • 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? –  Apr 04 '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 04 '12 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 Apr 06 '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

3 Answers3

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.

Sam Woods
  • 8,569
  • 24
  • 43
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

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.

Scott
  • 397
  • 4
  • 15