Advanced Techniques

Explore more about locators in this lesson, and take your Selenium knowledge to the next level.

Chaining findElement to find child elements

Aside from using XPath for a page containing more than one element with the same attributes, we can also use the technique of chaining findElement to find a child element.

For instance, consider the following HTML code:

HTML:

<div id="div1">
  <input type="checkbox" name="same" value="on"> Same checkbox in Div 1
</div>  
<div id="div2">
  <input type="checkbox" name="same" value="on"> Same checkbox in Div 2
</div>

Here we can chain findElement to find the child element by:

driver.findElement(By.id("div2")).findElement(By.name("same")).click();

Get hands-on with 1400+ tech skills courses.