java - 如何使用 Selenium 和 Java 在谷歌搜索结果页面上单击带有文本的元素作为谷歌的 Gmail
问题描述
我正在编写我的第一个 selenium java 脚本,如下所示。
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class testClass {
WebDriver driver;
public void launchBrowser() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\acer\\Downloads\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.google.com/");
Thread.sleep(1000);
}
private void sendData() throws InterruptedException {
WebElement we1 = driver.findElement(By.name("q"));
we1.sendKeys("GMAIL");
we1.sendKeys(Keys.ENTER);
Thread.sleep(2000);
we1.findElement(By.xpath("//a[text()='Gmail by Google']")).click();
Thread.sleep(2000);
}
public static void main(String[] args) throws InterruptedException {
testClass obj=new testClass();
obj.launchBrowser();
obj.sendData();
}
}
它没有错误。但
we1.findElement(By.xpath("//a[text()='Gmail by Google']")).click();
不管用。它不会点击Google 的 Gmail。有没有其他方法可以做到这一点?
解决方案
推荐阅读
- python-3.x - 无法为绘图颜色配置 matplotlib rcparams
- mongodb - Mongoose 聚合子文档的属性并显示结果
- ibm-watson - 是否可以使用 IBM Watson NLU 获得表情符号的情感?
- angular - Ionic 4 http.get.subscribe 不存储价值
- sql - PL/SQL 试图将 tableID 显示为另一个表名称
- annotations - 我可以创建一个自定义 JUnit 5 注释 @Defect("Ticket-ID") 并将其映射到 @Tag("Ticket-ID")
- python - Pandas groupby 和减去行
- c# - c# 用来自多个 Linq 查询的数据填充 listView
- html - 如果我同时添加 Bootstrap 类“d-lg-block”和“d-none”,则哪个类显示属性适用于 div
- swift - 是否可以使用 switch 方法将 2 个字符串匹配在一起?