Independent-dependent concept in XPath Expression

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.

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)’)]/..

first parent.png

Common parent of model and price web element.

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

common parent.png

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]

price highlighted.png

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 !!




Table of Contents

Author: Amod Mahajan

8 thoughts on “Independent-dependent concept in XPath Expression

  1. I have identified xpath for Independent and Dependent Element.
    Xpath for Independent Element=//h2[text()=’Selenium Web Driver Practical Guide’]/../..
    Xpath for Dependent Element=//span[2][contains(text(),’719.00′)]

    After combining the resultant xpath for Independent Element and Dependent Element,it is displaying as “No Matching Nodes”.

  2. I am using Independent-Dependent concept for the following scenario:

    Under books (select book option)
    Search for selenium
    Check the price of the third book price from hardcoded price (variable)

    I have written the xpath for the independent element i.e//h2[contains(text(),’Selenium Web Driver Practical Guide’)]/../…But i am struggling to write the xpath for the dependent element price.How to proceed?

  3. Hi, I am using the independent-dependent concept to automate the following scenario:
    Under books (select book option)
    Search for selenium
    Check the price of the third book price from Hardcoded price (variable):

    I have written the xpath for the independent element,but i am struggling to write the xpath for the dependent element .ie price of the third book.Any help would be appreciated.

    Xpath of the Independent element
    String xp2=”//h2[contains(text(),’Selenium Web Driver Practical Guide’)]/../..//span[2]”;

    1. Hi Ranjan,

      Use below xpath:
      //h2[text()=’Selenium Web Driver Practical Guide’]/../../../../div[2]/div[1]/div[2]//a/span[2]
      It will work.

          1. //h2[contains(text(),’Selenium Web Driver Practical Guide’)]/../../../../div[2]/div[1]/div[2]/a/span[2]/text()

            Try above one. Both are working.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.