SeleniumUser
SeleniumUser

Reputation: 4177

How to write generic xpath?

I am trying to write generic XPath for the below sites. Both sites have same elements on UI, but DOM structure is different while interacting with elements.

Here I am trying to get the text of left eye and right eye quantity

I am getting a text when I am using below XPath for Norway and UK respectively but when I am trying to write generic XPath which will work on both sites I am facing an issue :

So I wrote below XPath will work on both sites but it's not.

//a[contains(@id,'qty-1')] 

Expected Behaviour:

I want Quantity for both left and right eye element on both sites with the same XPATH.

Upvotes: 0

Views: 586

Answers (1)

Sers
Sers

Reputation: 12255

For Norway there's subscribe and for UK payment blocks. For Norway there're two HTML div .one-off and .subscribe under .payment-option.row.two-eyes css selectors. For UK only .one-off.

If you want generic xpath for both, use below:

Left: //div[contains(@class,'payment-option')]//div[contains(@class,'-selected') and (@style='display: block;' or @style='')]//div[contains(@class,'-left-eye')]//span[@class='ui-selectmenu-status']

Right: //div[contains(@class,'payment-option')]//div[contains(@class,'-selected') and (@style='display: block;' or @style='')]//div[contains(@class,'-right-eye')]//span[@class='ui-selectmenu-status']

This xpath is depends on the position in the document and not "safe". My suggestion is to use separate with simple locators or use div[class$='-right-eye'] .ui-selectmenu-status css selector, find all elements and filter by visibility.

Upvotes: 1

Related Questions