4

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
BelovedFool
  • 435
  • 6
  • 17

2 Answers2

3

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')]"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

XPATH 2 allows to do case insensitive matches but most browsers have XPATH 1.

Your best bet will be to use combination of text in one XPATH. Like :

"//*[contains(text(), 'error') or contains(text(), 'ERROR') or contains(text(), 'erreur') or contains(text(), 'ERREUR')]"

OR

"//*[contains(text(), 'error')] | //*[contains(text(), 'ERROR')] | //*[contains(text(), 'erreur')] | //*[contains(text(), 'ERREUR')]"
Naveen
  • 770
  • 10
  • 22