我在使用 selenium 关闭登录请求时遇到问题。
我正在做一些自动测试的网站时常请求登录。
这是已知问题,您所要做的就是关闭登录请求,我正在尝试为此编写 Java 脚本。我的这段小代码似乎什么也没做。
WebDriver webDriverInstance = handl.browser;
webDriverInstance.switchTo().alert().dismiss();
webDriverInstance.switchTo().defaultContent();
有什么想法吗?
最佳答案
你可以做的是首先检查是否有弹出窗口
public boolean isAlertPresent() {
try{
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.alertIsPresent());
return true;
}
catch (NoAlertPresentException noAlert) {
return false;
}
catch (TimeoutException timeOutEx){
return false;
}
}
如果它存在那么你可以像这样接受它
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
或
JavascriptExecutor 为您效劳。请注意,您应该在单击调用警报的事件之前执行它。
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }");
注意:- 不要在点击调用警报确认框的事件后使用它。上面的代码默认将确认框设置为 true 意味着如果调用,您将接受/单击该页面上所有确认框上的确定。如果您想关闭警报,请将标志更改为 false
希望对你有帮助:)
关于java - 如何关闭登录请求窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45570150/