case insensitive xpath contains() possible ?

case insensitive xpath contains() possible ?

Asked on January 8, 2019 in XML.
Add Comment


  • 18 Answer(s)

    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')]"
    
    Answered on January 8, 2019.
    Add Comment

         Try the below code for case sensitive xpath contain() method,

    /html/body//text()[contains(translate(., 'TES', 'tes'), 'test')]
    
    Answered on January 8, 2019.
    Add Comment

    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')]
    
    Answered on January 8, 2019.
    Add Comment

    /html/body//text()[contains(translate(.,
    ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’,
    ‘abcdefghijklmnopqrstuvwxyz’),
    ‘test’)]

    Answered on January 26, 2019.
    Add Comment
    /html/body//text()[
      contains(
        translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),
        'test'
      )
    ]
    Answered on February 28, 2019.
    Add Comment
    /html/body//text()[contains(translate(., 
                                          'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
                                          'abcdefghijklmnopqrstuvwxyz'),
                       'test')]
    Answered on February 28, 2019.
    Add Comment

    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

    Answered on February 28, 2019.
    Add Comment

    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

    Answered on February 28, 2019.
    Add Comment
    1. import java.util.List;
    2. import org.openqa.selenium.By;
    3. import org.openqa.selenium.SearchContext;
    4. import org.openqa.selenium.WebDriver;
    5. import org.openqa.selenium.WebElement;
    6.  
    7. public class ByXpath extends By {
    8. private String locator;
    9. private String value;
    10. public ByXpath(String locator, String value) {
    11. this.locator = locator;
    12. this.value = value;
    13. }
    14.  
    15. @Override
    16. public WebElement findElement(SearchContext searchContext) {
    17. List<WebElement> list1 = ((WebDriver)searchContext).
    18. findElements(lowerCaseXpath());
    19.  
    20. if (list1.size() > 0)
    21. return list1.get(0);
    22. List<WebElement> list2 = ((WebDriver)searchContext).
    23. findElements(upperCaseXpath());
    24.  
    25. if (list2.size() > 0)
    26. return list2.get(0);
    27. throw new RuntimeException
    28. (“cannot find the element with xpath “ +
    29. String.format(locator, value.toLowerCase()));
    30. }
    31.  
    32. @Override
    33. public List<WebElement> findElements(SearchContext searchContext) {
    34. List<WebElement> list1 = ((WebDriver)searchContext).
    35. findElements(lowerCaseXpath());
    36.  
    37. if (list1.size() > 0)
    38. return list1;
    39. List<WebElement> list2 = ((WebDriver)searchContext).
    40. findElements(upperCaseXpath());
    41.  
    42. if (list2.size() > 0)
    43. return list2;
    44. throw new RuntimeException
    45. (“cannot find elements with xpath “ +
    46. String.format(locator, value.toLowerCase()));
    47. };
    48. private By lowerCaseXpath() {
    49. return By.xpath(
    50. String.format(locator, value.toLowerCase()));
    51. }
    52. private By upperCaseXpath() {
    53. return By.xpath(
    54. String.format(locator, value.toUpperCase()));
    55. }
    56. }
    Answered on February 28, 2019.
    Add Comment

    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

    Answered on February 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.