Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an OR condition in Xpath to identify the same element

I have this logic which gets the current page's title first clicks on next button, fetches the title again and if both the titles are the same, meaning navigation has not moved to the next page, it clicks on Next again.

However, my problem is that the title element's Xpath differs - the same title element has two Xpaths. One is some pages the other in some other pages.

It is either this,

(.//span[@class='g-title'])[2]

OR

.//span[@class='g-title']

So, how can I handle this?

like image 653
whydoieven Avatar asked Dec 30 '15 05:12

whydoieven


People also ask

How to identify the element using XPath functions by attribute?

Identifying The Element Using The XPath functions By Attribute. We use the XPath functions (contains or starts-with) with attribute when there are some uniquely identified attribute values available in the container tag. Attributes are accessed using the “@” symbol. This can be understood clearly with the given example: Login to Google

What is the use of contains method in XPath?

Contains is one of the functions in XPath expressions. It can be used when the UI element attribute values are changing dynamically, for example: Dates, login successful/error messages, etc. Contains method can be used when we know about the partial value of an attribute or partial text associated with the web element, for example:

How to use partial constant visible text in XPath?

Make use of “starts-with ()” method in XPath if you know the initial partial constant visible text or attribute. You can also use contains () and starts-with () method with absolute text or attribute. Make use of the “text ()” method in XPath if you are aware of the absolute visible text. You can’t use text () method with the partial text.

How do you use XPath in an XML document?

We can use XPath to generate attribute expressions to locate nodes in an XML document. When there are certain uniquely recognized attribute values accessible in the container tag, we employ the XPath functions (contains or starts-with) with the attribute. The “@” symbol is used to access attributes.


2 Answers

If the element has two xpath, then you can write two xpaths like below

xpath1 | xpath2

Eg: //input[@name="username"] | //input[@id="wm_login-username"]

It will choose any one xpath

like image 145
Subrahmanya Prasad Avatar answered Oct 14 '22 14:10

Subrahmanya Prasad


You can use or operator like

(.//span[@class='g-title'])[2] or .//span[@class='g-title']

For more details : Two conditions using OR in XPATH

like image 44
Ajinkya Avatar answered Oct 14 '22 13:10

Ajinkya