Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.4k points)

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.


<div id="a">


        <span />

        <input />



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 descendent node. How can I ask in xpath for "the <div> that contains a <span> with the text "hello world'"?

1 Answer

0 votes
by (27.5k points)

The following command can be used in this scenario:

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

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s online automation testing training

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

29.3k questions

30.6k answers


104k users

Browse Categories