如何使用Selenium&处理浏览器登录弹出窗口爪哇 [英] How to handle browser login popup using Selenium & Java
问题描述
我要自动执行的网站上有一个身份验证弹出窗口,当有人访问它时会出现.我尝试了以下代码,但Chrome立即在switchTo()
行上未引发任何警报出现异常.
The website that I am trying to automate has a authentication popup that appears when someone visits it. I tried the following code but Chrome immediately throws no alert present exception on the switchTo()
line.
Firefox似乎无法正常工作.我目前正在将Selenium 3.6.0与这些浏览器的最新二进制文件一起使用.有人可以帮我吗?
Firefox does not seem to be working. I am currently using Selenium 3.6.0 with the latest binaries of these browsers. Can someone please help me out?
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://staging.brightsociety.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.authenticateUsing(new UserAndPassword(username, password));
driver.switchTo().defaultContent();
http://staging.brightsociety.com
推荐答案
Selenium无法通过switchto命令来处理它,因为这是特定于浏览器而非网站的内容.
Selenium do not handle it by switchto command because this is something that is browser specific instead of website.
解决方案1:
driver.get(" http://username:password@url.com ");
driver.get("http://username:password@url.com");
但是这种方法的问题是,当您遍历多个页面时,弹出窗口会一次又一次地出现,这又很难处理.
But problem with this method is that as you traverse to multiple pages the popup will appear again and again which is again difficult to handle.
解决方案2 :(推荐)
Solution 2: (Recommended)
AutoIt脚本,在此处下载 并找到用于身份验证弹出窗口的脚本
AutoIt script, Download AutoIt here And Find the Script for Authentication PopUp here
这篇关于如何使用Selenium&处理浏览器登录弹出窗口爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!