Srinu Reddy
Srinu Reddy

Reputation: 5

How to ignore case sensitive in this xpath c# selenium

I have a simple xpath:

driver.findelement(by.xpath("//li[contains(text(), 'chain')]").click() 

This code is working but its not recognize chain in uppercase, how to ignore case sensitive in this xpath?

Upvotes: 0

Views: 1187

Answers (3)

Vitaliy Moskalyuk
Vitaliy Moskalyuk

Reputation: 2583

Use translate, but to make it shorter, you may translate only characters you are looking for

//li[contains(translate(text(), 'CHAIN', 'chain'), 'chain')]

If you are going to use this a lot, you may even write a method for that. I'll write in Java (sorry, not familiar with C#):

public static void main(String[] args) {
    String l = "//li[" + containsTextIgnoringCase("StackOverflow") + "]";
    System.out.println(l);
}

public static String containsTextIgnoringCase(String s) {
    return String.format("contains(translate(text(), '%s', '%s'), '%s')", s.toUpperCase(), s.toLowerCase(), s);
}

output:

//li[contains(translate(text(), 'STACKOVERFLOW', 'stackoverflow'), 'stackoverflow')]

There's some space for optimization (not sure if it costs the effort, but still): translate only unique chars, and handle quotes, if passed in string

Upvotes: 0

frianH
frianH

Reputation: 7563

You can use the contains and translate functions together like this:

//li[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'chain')]

Upvotes: 1

Sh.Imran
Sh.Imran

Reputation: 1033

Use the value of chain in the way that assign it to a separate string variable and then apply .ToUpper() extension on it.

string strChain = "chain";
string getUpper = strChain.ToUpper();

It will give you cain in uppercase.

You can use it directly inside driver.findelement.

Upvotes: 0

Related Questions