5

I'm generating a List of WebElements with:

List<WebElement> elements = driver.findElements(By.className("name")); 

Now I'd like to get subelements from each element in elements. I tried solutions like

WebElement element = elements.get(0).findElement(By.xpath("/x")); 

where x is the tagname of each element in elements. But this searches in the complete active frame and not only in the subelements of elements.get(0)

Any idea?

GBa
  • 17,509
  • 15
  • 49
  • 67
user3561614
  • 1,024
  • 1
  • 12
  • 20
  • What do you want to achieve here? – Paras Jun 04 '16 at 14:02
  • I would like to find a WebElement, which is a child (child like child in XML) of another given WebElement. What I know about the child is the relative position to it's parent. – user3561614 Jun 04 '16 at 14:14
  • every element inside `elements` list is what you are looking for. So you now have the `WebElement` that you are looking for. – Paras Jun 04 '16 at 14:31
  • This is only true for my example. In fact I'm looking for some special childs, which I can locate with XPath relative to the WebElements I have (all elements in elements). – user3561614 Jun 04 '16 at 14:35
  • 3
    You might want to have a look at [Locating child nodes of WebElements in selenium](http://stackoverflow.com/questions/10520294/locating-child-nodes-of-webelements-in-selenium). It involves using a *relative* XPath expression. – halfbit Jun 04 '16 at 16:10
  • That I was looking for, could you write this as answer so that I can accept it? – user3561614 Jun 04 '16 at 16:28

1 Answers1

5

Probably all that you need is change the code to :

WebElement element = elements.get(0).findElement(By.xpath(".//x")); //this would search any **x** as a sub-child of your list's elements
Naman
  • 27,789
  • 26
  • 218
  • 353