5

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?

Tarken
  • 2,072
  • 2
  • 23
  • 42
  • XPath comes to mind: //*[lower-case(text())='linktext'] – Anders Dec 06 '11 at 18:18
  • I tried but your expression is invailid: org.openqa.selenium.InvalidSelectorException: The given selector //*[lower-case(text())='Linktext'] is either invalid or does not result in a WebElement. The following error occurred: [InvalidSelectorError] Unable to locate an element with the xpath expression //*[lower-case(text())='Linktext'] because of the following error: [Exception... "The expression is not a legal expression." code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)" location: "resource://fxdriver/modules/atoms.js Line: 2403"]; duration or timeout: 20 milliseconds – Tarken Dec 09 '11 at 08:48
  • The error would indicate that the browser does not support the latest XPath specification - take a look at this question for an alternative solution: http://stackoverflow.com/questions/1625446/problem-with-upper-case-and-lower-case-xpath-functions-in-selenium-ide/1625859#1625859 – Anders Dec 09 '11 at 15:36

3 Answers3

0

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();
        }
    }
}
djangofan
  • 26,842
  • 57
  • 182
  • 275
0

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.

jro
  • 8,880
  • 2
  • 30
  • 36
-2

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"));
nilesh
  • 13,751
  • 6
  • 62
  • 77
  • Then tell me how can I click my element if the linkText is LINKTEXT? The else code is relevant, because if no linkText linktext ist found the elements linkText is LINKTEXT and I can click it. – Tarken Dec 09 '11 at 08:41
  • By 'irrelevant' I meant it is not required. As far as clicking on element with linktext 'LINKTEXT', you already know the answer :) driver.findElement(By.linkText("LINKTEXT")).click(); – nilesh Dec 13 '11 at 01:13
  • Before you downvote, please put in a comment so that this answer can be useful – nilesh Apr 04 '18 at 05:49