Selenium with Javascript – Relative XPath Part 2

Selenium with Javascript – Relative XPath Part 2

We had discussed about different type of Relative XPath in out last session, where we were writing xpath for a particular element based on the attribute and tag of the Element.

So, we had seen different types of Relative XPath so that we can get a unique XPath for an Element which helps in selecting the exact element during the execution of Automation Test Cases.

Now, if suppose, we face below problems

  1. Element doesn’t have any attribute
  2. We are not able to find a unique XPath using the attributes of the Element

then what should we do, Should we go for Absolute XPath, and the answer is a Big No.

So, In this case, we need to find the unique XPath for some near by Element of the Target Element for which we want to find Relative XPath. Once we get a Unique XPath of the near by Element, Then We can find the unique XPath of the Target Element as well.

So, Let’s see it now. Let’s go to our demo ecommerce website and try to find XPath for the products displayed in the Home Page.

Let’s focus on the first Product and try to get the XPath for the image of that Product.

//a[@class='woocommerce-LoopProduct-link woocommerce-loop-product__link']/img - This is not a unique XPath and is going to point out many Elements

//div[@class='astra-shop-thumbnail-wrap']/a/img - This is not a unique XPath and is going to point out many Elements

//ul[@class='products columns-4']/li[1]/div[1]/a[1]/img - This is a unique XPath and is going to point out only the Target Element.

Let’s go to the PDP of our demo ecommerce website, and try to find unique XPath of an Element using the attribute of some near by element. If we go to Description tab, then we find that there are qualities of the Product are displayed, Let’s try to find out the XPath of any one Quality

//li[@class='detail-list'] - This is not a unique XPath and is going to point out many Elements

//ul[@class='prod-list']/li[3] - This is a unique XPath and is going to point out only the Target Element.
Admin

Working Professional with more than 12 Years of Experience in Software Testing with Automation and Manual Testing Knowledge.

Recent Posts

Why we can not over ride Constructor

Why we can not over ride a Constructor This is a very important question, that,…

55 years ago

Javascript Tutorial – Arrow Function in Javascript

What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…

55 years ago

Javascript Tutorial – Anonymous Function in Javascript

What is Anonymous Function Anonymous Function is a type of function which has no name…

55 years ago

Javascript Tutorial – Map, Filter and Reduce

What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…

55 years ago

Javascript Tutorial – Different ways of declaring or defining function in Javascript

9 different ways of declaring or defining a function in Javascript In, one of our…

55 years ago

Javascript Tutorial – Loops with Arrays in Javascript

Loops with Arrays in Javascript In our last session, we had seen, What is Array…

55 years ago