Home > other >  How to create case insensitive xpath in Selenium Java
How to create case insensitive xpath in Selenium Java

Time:04-08

I have created below XPath but it is not working as expected.

I am checking text in the cell of a html table and I want a particular cell to be matched.

I have created below XPath but it is not working.

*//table[@id="customerPortalTable"]//tbody//tr[td[ contains(translate(text(), "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), "{0}") ]]

{0} - This is the place holder for value to search

I want to match below values

Test Account or TEST ACCOUNT or Test ACCOUNT

<td >Test Account</td>
<td >Test ACCOUNT</td>
<td >TEST ACCOUNT</td>

CodePudding user response:

To create the case insensitive you can use the following solution:

"//table[@id="customerPortalTable"]//tbody//tr[.//td[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{}')]]".format(placeholder)

Effectively, your line of code will be:

placeholder= "Test Account"
WebElement element = driver.findElement(By.xpath("//table[@id="customerPortalTable"]//tbody//tr[.//td[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{}')]]".format(placeholder)));

References

You can find a couple of relevant detailed discussions in:

CodePudding user response:

Generally your XPath looks ok to me. Maybe the issue is connected with whether you search uppercase or lowercase, for example these both are returning the three td nodes:

 //td[contains(translate(text(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 'TEST ACCOUNT')]
 
 //td[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'test account')]
  • Related