Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find an element by Xpath which contains text

Tags:

c#

selenium

xpath

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')]")); 
like image 266
Naughty Ninja Avatar asked Jul 28 '15 10:07

Naughty Ninja


People also ask

Can XPath locate element using text?

text(): A built-in method in Selenium WebDriver that is used with XPath locator to locate an element based on its exact text value.

How find XPath text contains?

The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]

How will you write XPath if the tag has only text?

How will you write XPath if the tag has only text? We will start to write XPath with //, followed by input tag, then we will use the select attribute, followed by its attribute name like name, id, etc, and then we will choose a value of attribute in single quotes. Here, (1 of 1) means exact match.


2 Answers

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')]
like image 99
splash58 Avatar answered Nov 14 '22 22:11

splash58


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.

like image 36
Vishal Jagtap Avatar answered Nov 14 '22 21:11

Vishal Jagtap