简体   繁体   中英

Selecting a link with Selenium Webdriver?

How can I select a link with selenium webdriver?

Selenium before would be done by:

    selenium.click("link=Users");

But how can I do the same with webdriver?

I thought about

    driver.findElement(By.partialLinkText("Users")).click();

but this does not work. No link is clicked!

<html>
<body>
<div id="mainpage" class="mainpage">
<div id="pageid" class="pageid">
<div id="body">

<table>
<tbody>
<tr>
<td>
<table>
<tbody>
<tr>
<td>
<div id="id_menu" class="mymenu">
<ul>
<li class="li_class ">
<a href="/user.xhtml">Users</a>

stacktrace:

    org.openqa.selenium.NoSuchElementException: Unable to locate element: 
{"method":"partial link text","selector":"Users"} Command duration or timeout: 11.36 
seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions
/no_such_element.html Build info: version: '2.24.1', revision: '17205', time: '2012-06-19 
17:28:14' System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', 
java.version: '1.7.0_02' Driver info: driver.version: RemoteWebDriver Session ID: 
178449d1-4ff3-44f3-b35c-6a158db7c430 error at line: 34

This should work:

driver.findElement(By.LinkText("Users")).click();

By LinkText is possible

XPath is one of the most exact ways to point the element.

Try this:

driver.findElement(By.XPath("//li[@class='li_class']/a")).Click();

Using a CSS selector:

a[href*=user.xhtml]

Here are some tips for writing cssSelector

 = --> Equals string
^= --> Starts with string
$= --> Ends with string
*= --> Contains 
~= --> Contains in a list

I agree with Christoph - Link text should work. But I follow a different approach which works for me all the time.

All the element that I need to locate or select I give them an id (without CSS there won't be any difference in view). This helps in readability of my test cases, writing functions for general stuff and improves maintainability of the code. Only for the dynamic generated code or places where I can't use id I go for different approach.

我认为这会奏效:

driver.findElement(By.xpath("//a[@href='/user.xhtml']")).click();

I also had the issue that LinkText and LinkPartialText didn´t work. This was because I uset then HTMLUnit Driver. Using FireFox both methods works fine.

In my case chromedriver not allowed to click link, due to form obtain click. I was able to fix this by using:

if(driver.toString().contains("chrome")) {
        WebElement form=driver.findElement(By.id("form_id"));
            ((JavascriptExecutor)driver)
            .executeScript("arguments[0].setAttribute('style', 'display: block;')", form); //here I change visibility of element
    }

I had similar problem using PHP Webdriver. LinkText or partialLinkText weren't working, but text provided for search was equal to this in SOURCE code. (lets say it was link text: "Users")

I was scratching my head why it is not working, when everywhere else it was. Then I saw the difference. In fact in source code link text was Users, but on display it was modified by css text-transform to lower case, so it was displayed as "users". When I changed my search criteria from Users to users it worked like harm!

So remember - webdriver actually works on data it see. Didn't have idea it is case sensitive at all! But it actually worked and solved my problem. Cheers!

Try this out:

package mypack;
import java.util.List;

import org.openqa.selenium.By;

import mypackage.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

@SuppressWarnings("unused")
public class classnew {

    private static FirefoxDriver driver;

    public static void main(String[] args) {
        //String baseUrl = "http://newtours.demoaut.com/";
      FirefoxDriver Firefoxdriver = new FirefoxDriver();

        driver = null;
        driver.get("http://newtours.demoaut.com");

        String linkText1 = driver.findElement(By.partialLinkText("egis")).getText();
        System.out.println(linkText1);
        String linkText2 = driver.findElement(By.partialLinkText("EGIS")).getText();
        System.out.println(linkText1);  
        driver.quit();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM