在网站完全打开之前尝试放入登录凭据时“无警报打开”

在此处输入图像描述 我正在尝试访问一个网站,但是要首先访问该网站我需要输入用户名和密码,之后完整的网站将被打开。 当我尝试通过“alert.sendkeys”发送用户名和密码时,它会显示“No alert open”错误。 请注意网站只有在您首先输入用户名和密码然后点击“登录”进行授权才会打开。我的浏览器是chrome,版本:64,Chrome驱动程序版本是:2.38。 这是我的代码:

public static void launchWebsite(String URL) { driver.get(URL); try { WebDriverWait wait = new WebDriverWait(driver, 2); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); System.out.println(alert.getText()); alert.sendKeys("website"); alert.sendKeys("grren"); } catch (Exception e) { System.out.println("No alertis present"); //exception handling } } 

driver.get("http://UserName:Password@Example.com"); 如果站点使用基本身份validation,应该这样做。

特别说明:如果您的密码中有@,请不要忘记用%40替换那个坏男孩

如果您的身份validation服务器需要域名为“domainuser”的域名,则需要在url中添加双斜杠: //localdomain\user:password@example.com : //localdomain\user:password@example.com

编辑:

Chrome 59已取消对https:// user:password@example.comurl的支持。

从selenium 3.4开始,您可以使用以下方法在IE中处理它

 WebDriverWait wait = new WebDriverWait(driver, 10); Alert alert = wait.until(ExpectedConditions.alertIsPresent()); alert.authenticateUsing(new UserAndPassword(username, password)); 

有使用Chrome扩展程序处理此问题的方法 。 可以在这里找到更多解释

要么

将您的chrome版本降级到<59