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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24304752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:24:22  来源:igfitidea点击:

How to handle authentication popup with Selenium WebDriver using Java

javaauthenticationseleniumselenium-webdriverpopup

提问by Imen CHOK

I'm trying to handle authentication popup using the code below:

我正在尝试使用以下代码处理身份验证弹出窗口:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.http.phishy-userpass-length", 255);
profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "x.x.x.x");
driver = new FirefoxDriver(profile);
baseUrl="http://" + login + ":" + password + "@" + url;
driver.get(baseUrl + "/");

When I execute the test, the page shows the authentication popup and still loading for a until I click cancel button. A that moment, I can access to the next page ,this mean that the authentication success but still always show the authentication popup

当我执行测试时,页面会显示身份验证弹出窗口,并且在我单击取消按钮之前仍在加载。那一刻,我可以进入下一页,这意味着身份验证成功但仍然总是显示身份验证弹出窗口

回答by Raghav Arora

Don't use firefox profile and try below code:

不要使用 Firefox 配置文件并尝试以下代码:

driver.get("http://UserName:[email protected]");

If you're implementing it in IE browser, there are certain things which you need to do.

如果你在 IE 浏览器中实现它,你需要做一些事情。

In case your authentication server requires username with domain like "domainuser" you need to add double slash /to the url:

如果您的身份验证服务器需要像“domainuser”这样的域的用户名,您需要/在 url 中添加双斜杠:

//localdomain\user:[email protected]

回答by Prashanth Sams

The Alert Method, authenticateUsing()lets you skip the Http Basic Authenticationbox.

警报方法,authenticateUsing()让您跳过Http 基本身份验证框。

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

As of Selenium 3.4it is still in beta

Right now implementation is only done for InternetExplorerDriver

Selenium 3.4 开始,它仍处于测试阶段

现在实施只为 InternetExplorerDriver

回答by Mukesh Otwani

I faced this issue a number of times in my application.

我在申请中多次遇到这个问题。

We can generally handle this with the below 2 approaches.

我们通常可以使用以下 2 种方法来处理此问题。

  1. Pass the username and password in url itself

  2. You can create an AutoIT Script and call script before opening the url.

  1. 在 url 本身中传递用户名和密码

  2. 您可以在打开 url 之前创建一个 AutoIT 脚本并调用脚本。

Please check the below article in which I have mentioned both ways:
Handle Authentication/Login window in Selenium Webdriver

请查看下面我提到的两种方式的文章:
在 Selenium Webdriver 中处理身份验证/登录窗口

回答by Ripon Al Wasim

This should work for Firefox by using AutoAuth plugin:

这应该适用于使用AutoAuth 插件的Firefox :

FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
File ffPluginAutoAuth = new File("D:\autoauth-2.1-fx+fn.xpi");
firefoxProfile.addExtension(ffPluginAutoAuth);
driver = new FirefoxDriver(firefoxProfile);

回答by cjungel

If you have to deal with NTLM proxy authentication a good alternative is to use a configure a local proxy using CNTLM.

如果您必须处理 NTLM 代理身份验证,一个不错的选择是使用CNTLM配置本地代理。

The credentials and domain are configured in /etc/cntlm.conf.

凭据和域在/etc/cntlm.conf.

Afterwards you can just use you own proxy that handles all the NTLM stuff.

之后,您可以使用自己的代理来处理所有 NTLM 内容。

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:3128");
capabilities.setCapability(CapabilityType.PROXY, proxy);

driver = new ChromeDriver(capabilities);

回答by user6939

Try following solution and let me know in case of any issues:

尝试以下解决方案,如有任何问题,请告诉我:

driver.get('https://example.com/')
driver.switchTo().alert().sendKeys("username" + Keys.TAB + "password");
driver.switchTo().alert().accept();

This is working fine for me

这对我来说很好用

回答by Bhuvanesh Mani

Popular solution is to append username and password in URL, like, http://username:[email protected]. However, if your username or password contains special character, then it may fail. So when you create the URL, make sure you encode those special characters.

流行的解决方案是在 URL 中附加用户名和密码,例如http://username:[email protected]。但是,如果您的用户名或密码包含特殊字符,则可能会失败。因此,在创建 URL 时,请确保对这些特殊字符进行编码。

String username = URLEncoder.encode(user, StandardCharsets.UTF_8.toString());
String password = URLEncoder.encode(pass, StandardCharsets.UTF_8.toString());
String url = “http://“ + username + “:” + password + “@website.com”;
driver.get(url);