Skip to main content

Selenium Get First, Last and Nth Child Element using CSS selector

In selenium testing locating elements based on the child is pretty convenient. The CSS selectors provide various ways to find child elements from parent elements.

This elements tracking in based on a child can be done by 3 ways in Selenium using CSS Selector:

1. Selenium Get First Child Element

Locates the first child/element under the parent tag.





2. Get last child element in Selenium

Locates the last child/element under the parent body.





3. Selenium CSS Selector nth-child

Locates the child/element based on the number we provide that is the position where the element is present under the parent tag.



Here, n starts from 1.




Enough talk let us dive into the real-time use of these selectors!

For example purpose, we will be automating

These will be the things we are gonna implement in our test:

  1. Visit the page.
  2. Click on the women tab using first child.
  3. Click on the T-shirt tab using last child.
  4. Click on the Dresses tab using nth child.

For this, we will be using the CSS selector child tracking method.

Let us first Track down the elements of the women tab, T-shirt tab, and Dresses tab using the child CSS selector method.

Now here selector covers the entire body of the three tabs under which they are present.

selenium css selector nth-child

So in this case it is the parent.

Under this ul tag, there are 3 li tags which are the respective tags of women, dresses, and T-shirts.
selenium get first child element
So using these we will create selectors based on the first, last and nth-child.

  • For Women:>li:first-child
  • For Dresses:>li:nth-child(2)
  • For Tshirt:>li:last-child

Example: Find First, Last and Nth Element in Selenium

public class AppTest 
    WebDriver driver;
    public void befClass()
        System.setProperty("",System.getProperty("user.dir") + "//drivers//chromedriver");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    public void LoginTest()
        WebElement firstChild = driver.findElement(By.cssSelector(">li:first-child"));;
        WebElement lastChild = driver.findElement(By.cssSelector(">li:last-child"));;
        WebElement nthChild = driver.findElement(By.cssSelector(">li:nth-child(2)"));;

Above code will executed on website Here

  • First child: This will find the first element in the menu, which is Women and click on it.
  • Last child: This will find the last element under the menu, that is, T-Shirts and click on it.
  • Nth Child: This will find the  nth element which is second menu item on site with link text Dresses and click on it.

Submitted by arilio666 on October 2, 2020

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.


At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.