'Is there a way to ignore upper case when trying to find a link by linkText with Webdriver?
I am using Selenium 2 Webdriver.
I want to click on a link but the link text can be "Linktext" or "LINKTEXT". Is ther a better way than that:
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();
}
API and google didnt really help me. Any ideas how to ignore upper case?
Solution 1:[1]
I don't know for sure for version 2, but Selenium 1 supports regular expressions in the matching. These can be marked as case-insensitive. If applicable, the following could work:
driver.findElements(By.linkText("regexpi:Linktext"));
The trailing i indicates a case insensitive match.
Solution 2:[2]
I've also been trying to figure this one out. I haven't solved it yet, but here is the rough draft of my concept. It looks like it will work but it doesn't ( I get a stale reference exception) . I just need to figure out how to get this kind of thing working :
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();
}
}
}
Solution 3:[3]
LinkText is case sensitive. Therefore if you have two links with different cases then they will be two different elements independent of each other. Your example code seems incorrect to me. The list below will never ever contain a link with text 'LINKTEXT' so the 'else' part is irrelevant and not required.
List<WebElement> list = driver.findElements(By.linkText("Linktext"));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jro |
| Solution 2 | djangofan |
| Solution 3 | nilesh |
