How to find elements through text ignoring the case using Selenium and Xpath

BelovedFool :

I'm using java version "1.8.0_191" and selenium 3.141.59.

I'm trying to find out if a page contains the word "error" or "erreur". Also, I want it to be case insensitive.

Finding a text is easy:

List<WebElement> elementList = driver.findElements(By.xpath("//*[contains(text(), 'error')]"));

By I'm having a harder time making it case insensitive. So far I tried this (inspired by this question):

List<WebElement> elementList = driver.findElements(By.xpath("/html/body//text()[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'error')]"));

But it return the following error:

org.openqa.selenium.WebDriverException: TypeError: Expected an element or WindowProxy, got: [object Text] {}

I also tried this:

 List<WebElement> elementList = driver.findElements(By.xpath("//*[contains(transate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'error')]"));

But it doesn't work either (since it's not a legal expression).

So, any idea in how to make this work?

DebanjanB :

To create a list of elements within an webpage containing the text error ignoring the upper/lower cases you can use the translate() function within an as follows:

  • Syntax:

    translate('some text','abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')
    
  • Line of Code:

    List<WebElement> elementList = driver.findElements(By.xpath("//*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'error')]"));
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=98840&siteId=1