Search code examples
htmlxmlxpathanchorweb-testing

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.


Solution

  • 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')]