【发布时间】:2018-10-03 18:36:14
【问题描述】:
我是 Selenium 自动化测试的新手,我正在做一些基本的自动化测试,例如在 Google 中搜索某些内容,然后单击搜索结果中所需的链接。
我制作的下面的代码一直有效,直到我得到测试方法。我无法从 Google 搜索页面中选择链接,但我的控制台上没有显示任何错误。所以我在这个特定的行上设置了一个线程,它提到它可以找到链接名称,但是链接名称在 html 代码中使用,因为我已经检查过 Google 检查。
我是否遗漏了一些明显的东西?我对 Selenium 比较陌生,因此感谢您提供任何帮助。我也尝试从这个用户响应中镜像一些代码 "How to click a link by text in Selenium web driver java" 但没有运气!
谢谢
package com.demo.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class MyFirstTestScript {
private static WebDriver driver;
public static void main (String[] args) {
SetUp();
testing();
}
// TODO Auto-generated method stub
@setup
public static void SetUp () {
driver = new FirefoxDriver();
driver.get("http://www.google.co.uk");
System.setProperty("webdriver.gecko.driver", "usr/local/bin/geckodriver");
driver.findElement(By.name("q")).sendKeys("BBC" + Keys.ENTER);
}
@Test
public static void testing() {
driver.findElement(By.partialLinkText("BBC - Home")).click();
}
}
【问题讨论】:
-
这是另一种方法,但您可以将搜索请求构建到您传递给 driver.get 的 URL 中。
-
尝试使用 By.linkText 而不是 PartialLinkText
-
@camel-man 我最初尝试过这种方法,但没有奏效
标签: java selenium selenium-webdriver web automation