Reputation: 5
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
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
Reputation: 7563
You can use the contains
and translate
functions together like this:
//li[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'chain')]
Upvotes: 1
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