如何使用 Selenium & 处理浏览器登录弹出窗口爪哇

How to handle browser login popup using Selenium amp; Java(如何使用 Selenium amp; 处理浏览器登录弹出窗口爪哇)

本文介绍了如何使用 Selenium & 处理浏览器登录弹出窗口爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试自动化的网站有一个身份验证弹出窗口,当有人访问它时会出现.我尝试了以下代码,但 Chrome 立即在 switchTo() 行上抛出没有警报存在异常.

Firefox 似乎无法正常工作.我目前正在使用 Selenium 3.6.0 和这些浏览器的最新二进制文件.有人可以帮帮我吗?

driver = new FirefoxDriver();driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);driver.get("https://staging.brightsociety.com/");WebDriverWait 等待 = new WebDriverWait(驱动程序, 10);wait.until(ExpectedConditions.alertIsPresent());警报警报 = driver.switchTo().alert();alert.authenticateUsing(new UserAndPassword(username, password));driver.switchTo().defaultContent();

解决方案

Selenium 不要通过 switchto 命令处理它,因为这是特定于浏览器而不是网站的东西.

解决方案 1:

driver.get("http://username:password@url.com");

但是这种方法的问题是,当您遍历多个页面时,弹出窗口会一次又一次地出现,这再次难以处理.

解决方案 2:(推荐)

AutoIt 脚本,下载 AutoIt 这里并找到 Authentication PopUp 的脚本 这里

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 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 do not handle it by switchto command because this is something that is browser specific instead of website.

Solution 1:

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.

Solution 2: (Recommended)

AutoIt script, Download AutoIt here And Find the Script for Authentication PopUp here

这篇关于如何使用 Selenium & 处理浏览器登录弹出窗口爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 Selenium & 处理浏览器登录弹出窗口爪哇

基础教程推荐