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,112
  • 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

4 Answers4

0

This will definitely help, as it worked for me

List<WebElement> els = driver.findElements( By.xpath( ".//a[@href!='']" ) );
    for ( WebElement el: els ) {
        if ( el.getText().toLowerCase().equals( bookDetails.getBookName().toLowerCase() ) ) {
           el.click();
           break;
        }
    }
cigien
  • 57,834
  • 11
  • 73
  • 112
0

An alternative would be the following:

// get all the a elements from the page via tagname
    List<WebElement> elementList = driver.findElements(By.tagName("a"));

    // iterate through the list and click on element where text equals to text
    for( WebElement ele : elementList){

        if(ele.getText().equals("LINKTEXT") || ele.getText().equals("Linktext")){
            ele.click();
        }

    }

or the following for loop:

for( WebElement ele : elementList){

        if(ele.getText().toLowerCase().equals("linktext")){
            ele.click();
        }

    }
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
  • 9,300
  • 2
  • 32
  • 37
-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
  • 14,131
  • 7
  • 65
  • 79
  • 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