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