Java 如何在 Chrome 浏览器的 seleniumWebdriver 中设置代理身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44940815/
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
How to set Proxy Authentication in seleniumWebdriver for Chrome Browser
提问by Avishka Perera
I'm trying to Automate a web application selenium 2.0 [webdriver+java].The web application is currently deployed in our UAT servers on our local network.My test cases are executing, but I have to manually enter the Proxy Authentication details for my Chrome instance at the start of the test execution. I have tried all the solutions provided on stack overflow but still, the authentication message pops out.
我正在尝试自动化 web 应用程序 selenium 2.0 [webdriver+java]。该 web 应用程序当前部署在我们本地网络上的 UAT 服务器中。我的测试用例正在执行,但我必须手动输入代理身份验证详细信息测试执行开始时的 Chrome 实例。我已经尝试了堆栈溢出时提供的所有解决方案,但仍然会弹出身份验证消息。
This is the code I'm using in my driver initializing process
这是我在驱动程序初始化过程中使用的代码
package com.misyn.ess.ui;
包 com.misyn.ess.ui;
import java.util.Arrays;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
/**
*
* @author User
*/
public class DriverClass {
private String baseUrl;
private String driverPath;
private String driverName;
private static WebDriver driver;
private static DriverClass driverClass;
private DriverClass() {
try {
baseUrl = "http://192.168.0.10:8282/ess";
driverPath = "E:\Work_Folder\SelTools\chromedriver.exe";
driverName = "webdriver.chrome.driver";
//Set the location of the ChromeDriver
System.setProperty(driverName, driverPath);
//Create a new desired capability
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Create a new proxy object and set the proxy
Proxy proxy = new Proxy();
proxy.setHttpProxy("192.168.0.200:3128");
proxy.setSocksUsername("avishka");
proxy.setSocksPassword("12345678");
//Add the proxy to our capabilities
capabilities.setCapability("proxy", proxy);
//Start a new ChromeDriver using the capabilities object we created and added the proxy to
driver = new ChromeDriver(capabilities);
//Navigation to a url and a look at the traffic logged in fiddler
driver.navigate().to(baseUrl);
// System.setProperty(driverName, driverPath);
// driver = new ChromeDriver();
// driver.get(baseUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Can anyone give me a solution how to give this proxy username and password thing from the application itself than manually entering details on the pop-up(Authentication), any help would be much appreciated.Thanks
谁能给我一个解决方案,如何从应用程序本身提供这个代理用户名和密码,而不是在弹出窗口(身份验证)上手动输入详细信息,任何帮助将不胜感激。谢谢
the currently answered one is only for
当前回答的仅用于
As of Selenium 3.4 it is still in beta Right now implementation is only done for InternetExplorerDriver
从 Selenium 3.4 开始,它仍处于测试阶段 现在仅对 InternetExplorerDriver 进行了实现
Where I'm using selenium 3.0 and Google Chrome as my web browser.
我使用 selenium 3.0 和 Google Chrome 作为我的网络浏览器。
采纳答案by Avishka Perera
public class DriverClass {
private String baseUrl;
private String driverPath;
private String driverName;
private static WebDriver driver;
private static DriverClass driverClass;
public DriverClass() {
try {
baseUrl = "http://192.168.0.10:8282/ess";
driverPath = "E:\Work_Folder\SelTools\chromedriver.exe";
driverName = "webdriver.chrome.driver";
System.setProperty(driverName, driverPath);
Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setSslProxy("192.168.0.200" + ":" + 3128);
proxy.setFtpProxy("192.168.0.200" + ":" + 3128);
proxy.setSocksUsername("avishka");
proxy.setSocksPassword("12345678");
DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
driver = new ChromeDriver(desiredCapabilities);
driver.get(baseUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The proxy setting has been added with desired capabilities to pass values to proxy authentication,worked finally
代理设置已添加所需的功能以将值传递给代理身份验证,最终工作
回答by Madhan
You can do via MultiPass for HTTP basic authentication
Download the extension from
https://chrome.google.com/webstore/detail/multipass-for-http-basic/enhldmjbphoeibbpdhmjkchohnidgnah
从https://chrome.google.com/webstore/detail/multipass-for-http-basic/enhldmjbphoeibbpdhmjkchohnidgnah下载扩展
Download the extension as crx. You can get it as crx from chrome-extension-downloader
将扩展下载为 crx。你可以从chrome-extension-downloader获取它作为 crx
After that the config is simple.
之后配置就很简单了。
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
/**
*
* @author Phystem
*/
public class ChromeAuthTest {
WebDriver driver;
public ChromeAuthTest() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
}
private void initDriver() {
ChromeOptions cOptions = new ChromeOptions();
cOptions.addExtensions(new File("MultiPass-for-HTTP-basic-authentication_v.crx"));
driver = new ChromeDriver(cOptions);
configureAuth(
"https://the-internet.herokuapp.com/basic_auth",
"admin",
"admin");
}
private void configureAuth(String url, String username, String password) {
driver.get("chrome-extension://enhldmjbphoeibbpdhmjkchohnidgnah/options.html");
driver.findElement(By.id("url")).sendKeys(url);
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.className("credential-form-submit")).click();
}
public void doTest() {
initDriver();
driver.get("https://the-internet.herokuapp.com/basic_auth");
System.out.println(driver.getTitle());
driver.quit();
}
public static void main(String[] args) {
new ChromeAuthTest().doTest();
}
}
I have used a sample sitefor testing.
我使用了一个示例站点进行测试。
Provide your url,username and password in the configure Auth function and try
在配置 Auth 功能中提供您的 url、用户名和密码并尝试
回答by shiuu
This code (from Avishka Perera's answer) does not work for me:
这段代码(来自 Avishka Perera 的回答)对我不起作用:
proxy.setSocksUsername("avishka");
proxy.setSocksPassword("12345678");
The username and password set in this way do not take effect for the http/https proxy - the Proxy Authentication box still popped up.
以这种方式设置的用户名和密码对http/https代理不起作用——仍然弹出代理验证框。
I'm using Selenium java 3.141.0, ChromeDriver 2.33 and chrome 70. What works for me is to follow Mike's answer here Selenium using Python: enter/provide http proxy password for firefox. Create the zip file, then add the extension like this:
我正在使用 Selenium java 3.141.0、ChromeDriver 2.33 和 chrome 70。对我有用的是按照 Mike 在此处使用 Python 进行的回答Selenium: enter/provide http proxy password for firefox。创建 zip 文件,然后像这样添加扩展名:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File("src/test/resources/proxy.zip"));
WebDriver driver = new ChromeDriver(chromeOptions);
One catch is that the above code will run into error if you set "--headless" argument because chrome in headless mode cannot have extension (Is it possible to run Google Chrome in headless mode with extensions?). If your Chrome runs in Docker container and cannot show the UI, then to get this solution work, you'll need to run with Xvfb instead of in headless mode.
一个问题是,如果设置“--headless”参数,上面的代码会出错,因为无头模式下的 chrome 不能有扩展(是否可以在无头模式下运行带有扩展的谷歌浏览器?)。如果您的 Chrome 在 Docker 容器中运行并且无法显示 UI,那么要使此解决方案起作用,您需要使用 Xvfb 而不是无头模式运行。
回答by Shrawan
The approach that worked perfectly fine for me is by using AutoIT.
对我来说非常有效的方法是使用 AutoIT。
Install autoIT and prepare a simple script as shown in the picture attached and execute the script file from your testscript using Runtime.getRuntime().exec("\YOUR_SCRIPT.exe") before navigating to the baseURL.
安装 autoIT 并准备一个简单的脚本,如所附图片所示,并在导航到 baseURL 之前使用 Runtime.getRuntime().exec("\YOUR_SCRIPT.exe") 从您的测试脚本执行脚本文件。
回答by SANJEEV RAVI
Simple method to add authenticated proxy using selenium wire in Both firefox and chrome
在 Firefox 和 chrome 中使用硒线添加经过身份验证的代理的简单方法
In python
在蟒蛇中
Step:1
第1步
pip3 install selenium-wire
Step:2
第2步
from seleniumwire import webdriver
from selenium import webdriver
step:3
步骤:3
Add proxy in below-mensioned format
以下列格式添加代理
proxy= "username:password@ip:port"
options = {'proxy': {'http': proxy, 'https': proxy, 'no_proxy': 'localhost,127.0.0.1,dev_server:8080'}}
step:4pass proxy as an argument
步骤:4将代理作为参数传递
CHROME
铬合金
driver = webdriver.Chrome(options=chrome_options, executable_path="path of chrome driver", seleniumwire_options=options)
Firefox
火狐
driver = webdriver.Firefox(seleniumwire_options=options, executable_path="path of firefox driver", options=firefox_options)
step:5Verify proxy applied by requesting the url https://whatismyipaddress.com/
步骤:5通过请求 url https://whatismyipaddress.com/验证应用的代理
time.sleep(20)
driver.get("https://whatismyipaddress.com/")
Note: But selenium log shows it runs in without proxy because we are using an external package to apply proxy.
注意:但是 selenium 日志显示它在没有代理的情况下运行,因为我们使用外部包来应用代理。