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?
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
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:
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With