44

I am using selenium to test my web application and I can successfully find tags using By.xpath. However now and then I need to find child nodes within that node.

Example:

<div id="a"> <div> <span /> <input /> </div> </div> 

I can do:

WebElement divA = driver.findElement( By.xpath( "//div[@id='a']" ) ) 

But now I need to find the input, so I could do:

driver.findElement( By.xpath( "//div[@id='a']//input" ) ) 

However, at that point in code I only have divA, not its xpath anymore... I would like to do something like this:

WebElement input = driver.findElement( divA, By.xpath( "//input" ) ); 

But such a function does not exist. Can I do this anyhow?

BTW: Sometimes I need to find a <div> that has a certain decendent node. How can I ask in xpath for "the <div> that contains a <span> with the text 'hello world'"?

5 Answers 5

87

According to JavaDocs, you can do this:

WebElement input = divA.findElement(By.xpath(".//input")); 

How can I ask in xpath for "the div-tag that contains a span with the text 'hello world'"?

WebElement elem = driver.findElement(By.xpath("//div[span[text()='hello world']]")); 

The XPath spec is a suprisingly good read on this.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. I did not know yet, that i could use xpaths within [] inside xpaths...
the latest version of the XPath spec can be found here
just beautiful!
6

If you have to wait there is a method presenceOfNestedElementLocatedBy that takes the "parent" element and a locator, e.g. a By.xpath:

WebElement subNode = new WebDriverWait(driver,10).until( ExpectedConditions.presenceOfNestedElementLocatedBy( divA, By.xpath(".//div/span") ) ); 

1 Comment

exactly what I was searching for in a completely other context. Thx.
1

For Finding All the ChildNodes you can use the below Snippet

List<WebElement> childs = MyCurrentWebElement.findElements(By.xpath("./child::*")); for (WebElement e : childs) { System.out.println(e.getTagName()); } 

Note that this will give all the Child Nodes at same level -> Like if you have structure like this :

<Html> <body> <div> ---suppose this is current WebElement <a> <a> <img> <a> <img> <a> 

It will give me tag names of 3 anchor tags here only . If you want all the child Elements recursively , you can replace the above code with MyCurrentWebElement.findElements(By.xpath(".//*"));

Hope That Helps !!

2 Comments

This actually worked. I don't know why someone gave it -1. Thanks mate for still keeping it.
Glad to see it was useful for someone
0

I also found myself in a similar position a couple of weeks ago. You can also do this by creating a custom ElementLocatorFactory (or simply passing in divA into the DefaultElementLocatorFactory) to see if it's a child of the first div - you would then call the appropriate PageFactory initElements method.

In this case if you did the following:

PageFactory.initElements(new DefaultElementLocatorFactory(divA), pageObjectInstance)); // The Page Object instance would then need a WebElement // annotated with something like the xpath above or @FindBy(tagName = "input") 

Comments

-1

The toString() method of Selenium's By-Class produces something like "By.xpath: //XpathFoo"

So you could take a substring starting at the colon with something like this:

String selector = divA.toString().substring(s.indexOf(":") + 2); 

With this, you could find your element inside your other element with this:

WebElement input = driver.findElement( By.xpath( selector + "//input" ) ); 

Advantage: You have to search only once on the actual SUT, so it could give you a bonus in performance.

Disadvantage: Ugly... if you want to search for the parent element with css selectory and use xpath for it's childs, you have to check for types before you concatenate... In this case, Slanec's solution (using findElement on a WebElement) is much better.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.