如何使用 Java 代码在 selenium 中为 Chrome 浏览器设置代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45288842/
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 for Chrome browser in selenium using Java code
提问by Praveen Medipally
I am trying to run my selenium java code to test a webpage. But webpage is not loading because of network restrictions. When I set the proxy manually and hit the url in browser it works fine. Now I need to pass those proxy setting while running the selenium code. Please help me on this.
我正在尝试运行我的 selenium java 代码来测试网页。但由于网络限制,网页无法加载。当我手动设置代理并在浏览器中点击 url 时,它工作正常。现在我需要在运行 selenium 代码时传递这些代理设置。请帮我解决这个问题。
I tried below code, but still it shows the same error:
我尝试了下面的代码,但仍然显示相同的错误:
Proxy p=new Proxy();
// Set HTTP Port to 7777
p.setHttpProxy("www.abc.com:8080");
// Create desired Capability object
DesiredCapabilities cap=new DesiredCapabilities();
// Pass proxy object p
cap.setCapability(CapabilityType.PROXY, p);
// Open firefox browser
WebDriver driver=new ChromeDriver(cap);
采纳答案by Praveen Medipally
Issue is resolved with below code -
问题已通过以下代码解决 -
Proxy proxy = new Proxy();
proxy.setHttpProxy("yoururl:portno");
proxy.setSslProxy("yoururl:portno");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
回答by Barney
DesiredCapabilities dc;
dc = DesiredCapabilities.chrome();
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "9090");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "9090");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--disable-extensions");
dc.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(dc);
回答by user3052604
Passing a Capabilities object to the ChromeDriver() constructor is deprecated. One way to use a proxy is this:
不推荐将 Capabilities 对象传递给 ChromeDriver() 构造函数。使用代理的一种方法是:
String proxy = "127.0.0.1:5000";
ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" + proxy);
WebDriver webDriver = new ChromeDriver(options);
回答by Slavik
Another way of doing it:
另一种方法:
boolean useProxy = true;
ChromeOptions options = new ChromeOptions().addArguments(
'--headless',
'--no-sandbox',
'--disable-extensions',
'--proxy-bypass-list=localhost');
if (useProxy) {
options.addArguments("--proxy-server=http://ProxyHost:8080");
}
WebDriver driver = new ChromeDriver(options);
See https://peter.sh/experiments/chromium-command-line-switches/for more Chrome switches
有关更多 Chrome 开关,请参阅https://peter.sh/experiments/chromium-command-line-switches/
回答by sgrillon
Passing a Capabilities object to the ChromeDriver() constructor is deprecated
. You can find new official doc here.
将 Capabilities 对象传递给 ChromeDriver() 构造函数是deprecated
. 你可以在这里找到新的官方文档。
ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setAutodetect(false);
proxy.setHttpProxy("http_proxy-url:port");
proxy.setSslProxy("https_proxy-url:port");
proxy.setNoProxy("no_proxy-var");
chromeOptions.setCapability("proxy", proxy);
driver = new ChromeDriver(chromeOptions);