java - 无法使用 selenium webdriver 和 Java 单击登录按钮
问题描述
嗨,我正在尝试自动化https://www.nextgenerationautomation.com并且无法使用 Selenium 4 单击登录/注册按钮
脚步:
- 导航到 URL:https ://www.nextgenerationautomation.com
- 单击登录/注册按钮。
问题:当我使用时Thread.Sleep
,代码工作正常,但是当我使用显式等待和隐式等待时,它不起作用。
我在驱动程序初始化时在我的基类中添加了隐式。
这是我尝试过的代码。
public class nextGenAutomationLoginPage extends Base {
@FindBy(xpath = "(//button[@class='_1YW_5'])[1]")
WebElement login;
public nextGenAutomationLoginPage() {
super();
PageFactory.initElements(driver, this);
// TODO Auto-generated constructor stub
}
public void clickOnLogin() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
System.out.println(driver.getTitle());
// Thread.sleep(2000);
wait.until(ExpectedConditions.elementToBeClickable(login));
//wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(login, By.xpath("//div[@class='_10b1I']") ));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
js.executeScript("arguments[0].scrollIntoView();", login);
login.click();
//driver.findElement(By.xpath("(//button[@class='_1YW_5'])[1]")).click();
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@id='comp-kaoxkn4a1']")));
String sign = driver.findElement(By.xpath("//div[@id='comp-kaoxkn4a1']/h1/span")).getText();
System.out.println(sign);
}
注意:我也尝试添加滚动以在单击之前添加一些等待。
解决方案
嗨,我明白这里发生了什么:
- 手动导航到 URL:https ://www.nextgenerationautomation.com (Selenium 也加载此 URL)
- 手动立即继续点击“登录/注册按钮”(Selenium 也可以点击,因此控制台中没有错误)
- 除非在此处输入图像描述(LinkedIn 以下计数小部件)出现在屏幕上,否则“登录/注册”页面不会打开
- 一旦在此处输入图像描述就会出现手动单击“登录/注册按钮”,现在登录页面正在打开(Selenium 可以在 Thread.Sleep 之后单击)
总结:
- 这是该页面上的编码缺陷。
- “登录/注册”不可点击,直到 在此处输入图像描述 被添加到页面上
- 你的硒代码非常好:)
- 我使用的 Xpath 是
//span[text()='Log In / SignUp']
谢谢KS
推荐阅读
- python-3.x - 尝试从脚本运行scrapy时出现“ImportError”
- c - C:通过绝对值函数传递数组
- firebase - SwiftUI + Firestore - 限制查询中的结果数量
- javascript - d3.js中选择指向的“this”在匿名函数和箭头函数之间有什么区别
- generics - Blazor 组件中的泛型类型参数可以受到约束吗?
- javascript - 根目录外的 webpack /src
- python - 从 API 响应解析 xml
- firebase - 即使使用合并选项,Firestore 也会覆盖数据
- javascript - 我怎样才能访问我知道动态键像树一样的对象
- python - 错误:'int' 对象没有属性 'lower' - 关于 CountVectorizer 和 Pandas