例外:等待警报出现

时间:2017-05-27 13:37:29

标签: java selenium selenium-webdriver selenium-chromedriver

我需要打开一个包含身份验证警报的网站。 为了处理该警报,我尝试了代码,但没有成功:

try {
    WebDriverWait wait = new WebDriverWait(driver, 100);
    wait.until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
     alert.authenticateUsing((Credentials) new UsernamePasswordCredentials("username", "pass"));
} catch (Exception e) {
    System.out.println("Alert not found");
}

发生异常是:

Expected condition failed: waiting for alert to be present (tried for 100 second(s) with 500 MILLISECONDS interval)

enter image description here

1 个答案:

答案 0 :(得分:0)

身份验证不是弹出窗口,我们可以使用AutoIT或Sikuli自动执行此操作。但这是一项繁琐的工作,幸运的是我们有另一个简单的解决方案。我们的想法是将用户名和密码与网址一起传递。

对于Eg ..,要传递用户名和密码,网址将为 http://username:password@url

Selenium Java代码:

driver.get("http://USERNMAE:PASSWORD@YOUR_URL");

希望这会对你有所帮助。感谢。

相关问题