选择Selenium Webdriver的链接?

mem*_*und 7 java selenium xpath selenium-webdriver

如何选择selenium webdriver的链接?

之前的Selenium将通过以下方式完成:

    selenium.click("link=Users");
Run Code Online (Sandbox Code Playgroud)

但是我怎么能用webdriver做同样的事情呢?

我想过

    driver.findElement(By.partialLinkText("Users")).click();
Run Code Online (Sandbox Code Playgroud)

但这不起作用.没有点击链接!

<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>
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

    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
Run Code Online (Sandbox Code Playgroud)

Iva*_*kov 7

XPath是指向元素的最精确方法之一.

试试这个:

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

  • XPath它也是最痛苦,最难以理解,不可持续的方式.我建议只使用链接文本或其他最简单/最语义的css选择器. (5认同)
  • xpath 为网络驱动程序提供了更多的支持。 (2认同)

Chr*_*ter 5

这应该工作:

driver.findElement(By.LinkText("Users")).click();
Run Code Online (Sandbox Code Playgroud)

通过LinkText是可能的