java - 为什么 Selenium 测试网站不能在多个页面上工作?
问题描述
代码工作的第一部分将起作用。但是第二部分不行,也没有出现错误,也不知道问题出在哪里。所以请帮忙。
第一部分是登录页面,第二部分是主页。
package Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
public class test {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.gecko.driver",
"E:\\Quality\\drivers\\geckodriver.exe");
driver =new FirefoxDriver();
driver.get("https://www.linkedin.com/uas/login");
// first part//
driver.findElement(By.xpath("//*[@id=\"session_key-login\"]")).click();
driver.findElement(By.xpath("//[@id=\"session_keylogin\"]")).sendKeys("Email");
driver.findElement(By.xpath("//*[@id=\"session_password-login\"]")).click();
driver.findElement(By.xpath("//*[@id=\"session_password-login\"]")).sendKeys("*******");
driver.findElement(By.xpath("//*[@id=\"btn-primary\"]")).click();
// second part//
WebElement test = null ;
test.findElement(By.xpath("/html/body/div[5]/div[5]/aside/div/header")).click();
}
}
解决方案
xpath
您为电子邮件文本框输入了错误的 ID 。
您应该使用session_key-login
而不是session_keylogin
.
只需在您使用sendKeys()
方法的地方使用以下修改后的代码:
driver.findElement(By.xpath("//*[@id=\"session_key-login\"]")).sendKeys("Email");
第二部分解决方案
跳过 WebElement 声明,因此注释该行 //WebElement test = null;
使用driver
对象的线
driver.findElement(By.xpath("/html/body/div[5]/div[5]/aside/div/header")).click();
您也可以使用 xpath //*[@id=\"msg-overlay\"]/div/header
,因此修改后的代码是:
driver.findElement(By.xpath("//*[@id=\"msg-overlay\"]/div/header")).click();
推荐阅读
- ios - 在添加和删除第二个按钮时保持 SwiftUI 中的导航项按钮对齐
- google-cloud-platform - 使用 gsutil CLI 4.51 时如何为 gslib 设置 USER_AGENT?
- python - 有没有办法使用 python 从数据框中填充本体?
- azure - 如何在 azure blob 存储中设置“Content-Disposition:附件”标头?
- android - 如何获取 firebase 存储的下载 url 和更新 firestore 文档?
- netsuite - Netsuite 项目履行给出“通知:不允许服务”
- python - 为什么我的 tkinter 滚动条没有按预期工作?我 hv 删除了滚动条的命令行
- javascript - Javascript递归循环单击github PR上的分页按钮
- c# - SQL查询以提取同一月份的日期数
- react-native - 获取 json 后函数返回错误的字符串,但 console.log 显示正确的字符串