Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Independent-dependent concept in XPath Expression

Posted on 02/19/2025 By admin

Hello Folks,

I will explain independent-dependent concept in writing XPath expression.

Consider below scenario:

You need to get the displayed price of iPhone 7 of all variants on Flipkart website. As we know price will vary based on model number, memory, color etc. Even price may change also with time or when some sale in on. But model name will be always constant. So, can we say that if we retrieve price based on model name, we will get always updated price.

Price is dependent on model number. Here we can say, model name is independent entity while price is dependent entity which depends upon respective model number.

Consider another scenario:

Suppose you have a form which asks details like Name, Age, Gender, Email id and mobile number and provided a text area with proper label. As a good automation tester, you should check the label before typing. So, first you need to check the label and then you need to go to corresponding input/textarea. This is also a scenario for independent-dependent concept.

How to write XPath expression for such element:

Step 1: Open Flipkart and search for iphone 7.

Step 2: Right click on model name and inspect it. It will highlight a div tag.

Step 3: Now move towards upper nodes just by using upper arrow key or touch pad till model name and its price both are highlighted. This will give you common parent of independent and dependent element.

Step 4: Now, write XPath to locate model name only as:

//div[contains(text(),’Apple iPhone 7 (Black, 32 GB)’)]

It will highlight only model name web element.

Concept: There is a 3 BHK flat. From hall you can go to any room. If you go inside a room, you can not go another room directly. You need to come out from room and go to hall and then you can go for another room.

Same concept applies here. You are inside div and you can navigate only to node inside div. You can not go any node which is outside of div.  Here price of model is in another div. So you must need to go to common parent i.e. hall to navigate to div.

You can traverse to immediate parent node by using double dots with slash. To reach common parent, you need to move up two times. So you need to use double dot with slash twice.

Parent of model name web element:

XPath: //div[contains(text(),’Apple iPhone 7 (Black, 32 GB)’)]/..

Common parent of model and price web element.

XPath: //div[contains(text(),’Apple iPhone 7 (Black, 32 GB)’)]/../..

Step 5: Now you have reached to common parent and you are able to go to any immediate children of common parent. As we can see there is two div element and price is in second div. So, we can navigate to directly second div using index.

XPath: //div[contains(text(),’Apple iPhone 7 (Black, 32 GB)’)]/../../div[2]

Step 6: Now we well very well how can you traverse to price web element.

XPath: //div[contains(text(),’Apple iPhone 7 (Black, 32 GB)’)]/../../div[2]/div[1]/div[1]/div[1]

That’s it. I think it will help you to understand independent-dependent concept. If you have any doubt feel free to ask me.

If you like my posts, please like, comment and share. Feedback and suggestions are always welcomed.

Thank you !!

#HappyLearning

Uncategorized

Post navigation

Previous Post: Where Does PageFactory Suck – Selenium WebDriver – Java
Next Post: Java Programs 14: Java Program to Print Fibonacci Series of Given Length Using Recursion

Related Posts

Common Mistakes & Best Practices in Selenium Uncategorized
Postman Tutorial Part 9 – DIfference Between Put and Patch HTTP Methods Uncategorized
REST Assured Tutorial 59 – How To Create JsonPath For Simple And Nested JSON Array? Uncategorized
TestNG Tutorials 68 : Sharing Data Between Tests in a Suite Using ISuite & ITestContext Uncategorized
image – Make Selenium Easy Uncategorized
How to validate paste disabled input box using Selenium WebDriver Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark