case insensitive xpath contains() possible ?
case insensitive xpath contains() possible ?
Case insensitive xpath contains():
/html/body//text()[ contains( translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'test' ) ]
If the users can do, then mark the portions of text that interest us with some other means, like enclosing them in a <span> which has a cassertive class.
If it is not possible, then we can have JavaScript that help with building an correct XPath expression,
function xpathPrepare(xpath, searchString) { return xpath.replace("$u", searchString.toUpperCase()) .replace("$l", searchString.toLowerCase()) .replace("$s", searchString.toLowerCase()); } xp = xpathPrepare("//text()[contains(translate(., '$u', '$l'), '$s')]", "Test"); // -> "//text()[contains(translate(., 'TEST', 'test'), 'test')]"
Try the below code for case sensitive xpath contain() method,
/html/body//text()[contains(translate(., 'TES', 'tes'), 'test')]
Solution for XPath 2.0:
- Use lower-case():
/html/body//text()[contains(lower-case(.),'test')]
- Use matches() regex matching with its case-insensitive flag:
/html/body//text()[matches(.,'test', 'i')]
/html/body//text()[contains(translate(.,
‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’,
‘abcdefghijklmnopqrstuvwxyz’),
‘test’)]
/html/body//text()[
contains(
translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),
'test'
)
]
/html/body//text()[contains(translate(.,
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz'),
'test')]
if the id value is lowercase.
I am not going to use the translate() or match() xpath functions for it because you need to apply them to every element value.
Instead, I will create a custom locator class which extends the By class.
The custom locator implements both the findElement() and findElements() methods.
Both methods work in a similar way:
- check if there are elements matched by the lowercase locator; if there are, return either the first of them or the whole list
- check if there are elements matched by the uppercase locator; if there are, return either the first of them or the whole list
The custom locator class works for simple xpath expressions with only 1 text value.
It can be changed though for more than 1 value
if the id value is lowercase.
I am not going to use the translate() or match() xpath functions for it because you need to apply them to every element value.
Instead, I will create a custom locator class which extends the By class.
The custom locator implements both the findElement() and findElements() methods.
Both methods work in a similar way:
- check if there are elements matched by the lowercase locator; if there are, return either the first of them or the whole list
- check if there are elements matched by the uppercase locator; if there are, return either the first of them or the whole list
The custom locator class works for simple xpath expressions with only 1 text value.
It can be changed though for more than 1
- import java.util.List;
- import org.openqa.selenium.By;
- import org.openqa.selenium.SearchContext;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- public class ByXpath extends By {
- private String locator;
- private String value;
- public ByXpath(String locator, String value) {
- this.locator = locator;
- this.value = value;
- }
- @Override
- public WebElement findElement(SearchContext searchContext) {
- List<WebElement> list1 = ((WebDriver)searchContext).
- findElements(lowerCaseXpath());
- if (list1.size() > 0)
- return list1.get(0);
- List<WebElement> list2 = ((WebDriver)searchContext).
- findElements(upperCaseXpath());
- if (list2.size() > 0)
- return list2.get(0);
- throw new RuntimeException
- (“cannot find the element with xpath “ +
- String.format(locator, value.toLowerCase()));
- }
- @Override
- public List<WebElement> findElements(SearchContext searchContext) {
- List<WebElement> list1 = ((WebDriver)searchContext).
- findElements(lowerCaseXpath());
- if (list1.size() > 0)
- return list1;
- List<WebElement> list2 = ((WebDriver)searchContext).
- findElements(upperCaseXpath());
- if (list2.size() > 0)
- return list2;
- throw new RuntimeException
- (“cannot find elements with xpath “ +
- String.format(locator, value.toLowerCase()));
- };
- private By lowerCaseXpath() {
- return By.xpath(
- String.format(locator, value.toLowerCase()));
- }
- private By upperCaseXpath() {
- return By.xpath(
- String.format(locator, value.toUpperCase()));
- }
- }
There any many other functions which you can try even.
1) Based on location
2) Using Single Attributes
3) Using Multiple Attributes
4) Using functions