Is there any way to ignore uppercase when trying to find linkText link with Webdriver?

I am using Selenium 2 Webdriver.

I want to click a link, but the link text can be "Linktext" or "LINKTEXT". Is this better:

List<WebElement> list = driver.findElements(By.linkText("Linktext")); if(list.size()>0){ driver.findElement(By.linkText("Linktext")).click(); } else { driver.findElement(By.linkText("LINKTEXT")).click(); } 

The APIs and google really didn't help me. Any ideas how to ignore uppercase?

+5
source share
3 answers

I do not know for sure for version 2, but Selenium 1 supports regular expressions in matching. They can be marked as case-insensitive. If applicable, the following may work:

 driver.findElements(By.linkText("regexpi:Linktext")); 

The final i indicates case insensitivity.

0
source

I'm trying to figure it out too. I have not decided this yet, but here is a rough draft of my concept. It seems like this will work, but it is not (I am getting an outdated reference exception). I just need to figure out how this works:

 public boolean clickByLinkTextInsensitive( String matcher ) { List<WebElement> els = driver.getElements( By.xpath( ".//a[@href!='']" ) ); for ( WebElement el: els ) { if ( els.getText().toLowerCase().equals( matcher.toLowerCase() ) ) { el.click(); } } } 
0
source

LinkText is case sensitive. Therefore, if you have two links with different cases, then they will be two different elements independently of each other. Your sample code seems wrong to me. There will never be a link in the list below with the text "LINKTEXT", so "else" does not matter and is not required.

 List<WebElement> list = driver.findElements(By.linkText("Linktext")); 
-1
source

Source: https://habr.com/ru/post/1384693/


All Articles