如何使用Java使用Selenium WebDriver处理Chrome中的身份验证弹出窗口

如何使用Java使用Selenium WebDriver处理Chrome中的身份验证弹出窗口

问题描述:

我正在尝试在一个新的Webdriver脚本中处理身份验证弹出窗口.我有一个适用于IE的有效解决方案,但我在Chrome方面苦苦挣扎. IE就像按照[本页]的建议一样简单:

I am trying to handle an authentication pop-up in one of my new Webdriver scripts. I have a working solution for IE, but I am struggling with Chrome. IE was as simple as following the advice on [this page]:How to handle authentication popup with Selenium WebDriver using Java. That thread doesn't show a great solution for Chrome, although several commentors point out, that the solution does not work for Chrome. The problem is, when you try to do the below code on Chrome, the login popup isn't an Alert.

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

这不是Windows级别()身份验证弹出窗口,网页仅受密码保护.我知道在Stack Overflow上还有这个问题的其他几个实例,但是最近不到2年了.我希望在2017年能有更好的解决方案.在此先感谢.

It's not a windows level () authentication pop-up, the web page is simply password protected. I know there are several other instances of this question on Stack Overflow, but I don't see any more recently than 2 years old. I am hoping there is a better solution now in 2017. Thanks in advance.

* edit Chrome不再支持此功能.

不是可以通过在地址前添加用户名和密码来处理受限制的"弹出窗口吗?

Isn't that a "restricted" pop-up that can be handled by prepending the address with username and password?

代替driver.get("http://www.example.com/");进入driver.get("http://username:password@www.example.com");.