如何在 Selenium Java 中为 Chrome 设置代理设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19225507/
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-12 15:13:35  来源:igfitidea点击:

How to set Proxy setting for Chrome in Selenium Java

javaselenium-webdriver

提问by user1140680

I am able to set proxy settings for Firefox as below.

我可以为 Firefox 设置代理设置,如下所示。

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setProxyType(ProxyType.MANUAL); 

proxy.setHttpProxy(CONFIG.getProperty("hostname"));
proxy.setSslProxy(CONFIG.getProperty("hostname"));
proxy.setFtpProxy(CONFIG.getProperty("hostname"));
proxy.setSocksUsername(CONFIG.getProperty("username"));
proxy.setSocksPassword(CONFIG.getProperty("password"));
FirefoxProfile fp = new FirefoxProfile();
fp.setProxyPreferences(proxy);

driver = new FirefoxDriver(fp);
builder = new Actions(driver); 
bckdbrowser = new WebDriverBackedSelenium(driver, ConfigReader.ENVIRONMENT_URL);

But I need to setup for Chrome as well.. Can any one assist me how to do ?

但我也需要为 Chrome 设置。任何人都可以帮助我怎么做吗?

Thanks Raj

谢谢拉吉

采纳答案by Farlan

You can try using the DesiredCapabilitiesclass, like this:

您可以尝试使用DesiredCapabilities该类,如下所示:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://user:[email protected]:8080"));
WebDriver driver = new ChromeDriver(capabilities);

回答by satender

Try this code:

试试这个代码:

FirefoxProfile profile = new FirefoxProfile(); 

profile.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal()); 

WebDriver driver = new FirefoxDriver(profile);

here we have one more solution....it's worked for me

在这里,我们还有一个解决方案......它对我有用