Find an element by Xpath which contains text

2020-02-26 12:39发布

问题:

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')]")); 

回答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')]


回答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.