繁体   English   中英

选择与 Selenium Webdriver 的链接?

[英]Selecting a link with Selenium Webdriver?

如何选择带有 selenium webdriver 的链接?

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

    selenium.click("link=Users");

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

我想过

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

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

<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>

堆栈跟踪:

    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

这应该有效:

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

通过 LinkText 是可能的

XPath 是指向元素的最准确的方法之一。

试试这个:

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

使用 CSS 选择器:

a[href*=user.xhtml]

这里有一些编写 cssSelector 的技巧

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

我同意 Christoph - 链接文本应该有效。 但我遵循一种不同的方法,这种方法一直对我有用。

我需要定位或选择的所有元素我都给了它们一个 id(没有 CSS,视图不会有任何区别)。 这有助于我的测试用例的可读性,为一般内容编写函数并提高代码的可维护性。 仅对于动态生成的代码或我不能使用 id 的地方,我才采用不同的方法。

我认为这会奏效:

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

我也遇到了 LinkText 和 LinkPartialText 不起作用的问题。 这是因为我当时使用了 HTMLUnit 驱动程序。 使用 FireFox 两种方法都可以正常工作。

在我的情况下,chromedriver 不允许点击链接,因为表单获得点击。 我能够通过使用来解决这个问题:

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
    }

我在使用 PHP Webdriver 时遇到了类似的问题。 LinkText 或 partialLinkText 不起作用,但为搜索提供的文本在源代码中与此相同。 (假设它是链接文本:“用户”)

我在挠头,为什么它不起作用,而其他地方却如此。 然后我看到了区别。 实际上源代码中的链接文本是Users,但是在显示时被css text-transform 修改为小写,所以显示为“users”。 当我将搜索条件从“用户”更改为“用户”时,它的作用就像伤害一样!

所以请记住 - webdriver 实际上可以处理它看到的数据。 根本不知道它是区分大小写的! 但它确实有效并解决了我的问题。 干杯!

试试这个:

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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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