Find an element by Xpath which contains text

36,115

Solution 1

say or between two calls of contains function

//a[contains(text(), 'About us') or contains(text(), 'about us')]

or use translate function to make xpath case insensitive

//a[contains(translate(text(), 'ABOUTS', 'abouts'), 'about us')]

Solution 2

Below satisfies your requirement:

//a[contains(., 'About us') or contains(., 'about us')] 

Refer:- https://sqa.stackexchange.com/questions/10342/how-to-find-element-using-contains-in-xpath for more details.

Share:
36,115
Naughty Ninja
Author by

Naughty Ninja

Updated on March 27, 2020

Comments

  • Naughty Ninja
    Naughty Ninja over 4 years

    This works fine if I search for a single string:

    var element = Driver.FindElement(By.XPath("//a[contains(text(), 'About us')]"));
    

    But could I have an or statement like in the example below?

    var element = Driver.FindElement(By.XPath("//a[contains(text(), 'About us' or 'about us')]")); 
    
  • har07
    har07 almost 9 years
    can be a little bit simplified to : translate(text(), 'ABOUTS', 'abouts')
  • splash58
    splash58 almost 9 years
    @har07 thanks. Specially looked up the repeating letters and missed anyway