user12606498
user12606498

Reputation:

XPath containing 2 or more "OR" conditions not working?

There is an anchor tag, whose value can be changed by the user. Now, i want to write an Xpath query that searches for multiple link text names in one statement itself.

<a href="#login" class="fancybox" xpath="1">Signin</a>

now, link text value can be changed by user profile. for eg, "Login", "Log-in", "Click here to login" or "Login now"

If i write xpath:-

//a[contains(text(),'Login') or contains(text(),'Log-in') or contains(text(),'Login now') or contains(text(),'click here to Login')]

Then it fails. I need to click this element using Selenium.

Please help.

Upvotes: 2

Views: 801

Answers (2)

kjhughes
kjhughes

Reputation: 111541

Important notes:

XPath 1.0

If you're certain there are no whitespace variations:

//a[.='Login' or .='Log-in' or .='Click here to login' or .='Login now']

Otherwise:

//a[   normalize-space()='Login" 
    or normalize-space()='Log-in' 
    or normalize-space()='Click here to login' 
    or normalize-space()='Login now']

XPath 2.0

//a[normalize-space()=('Login','Log-in','Click here to login','Login now')]

Upvotes: 5

Yitzhak Khabinsky
Yitzhak Khabinsky

Reputation: 22177

XPath is based on sequences. So you can use a comma separated list, i.e. sequence, to imitate logical OR conditions.

XPath

//a[text()=("Login","Log-in","Click here to login")]

Upvotes: 1

Related Questions