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

12

我可以像下面这样为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);

但我还需要为Chrome进行设置,有人可以帮我吗?

谢谢 Raj

2个回答

13
您可以尝试使用DesiredCapabilities类,像这样:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://user:password@proxy.com:8080"));
WebDriver driver = new ChromeDriver(capabilities);

你的意思是我需要原封不动地给出 --proxy-server 参数吗? - user1140680
capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://" + CONFIG.getProperty("username") + ":" + CONFIG.getProperty("password") + "@" + CONFIG.getProperty("hostname"))); 能力设置.设置能力("chrome.switches", 数组列表(Arrays.asList("--proxy-server=http://" + CONFIG.getProperty("username") + ":" + CONFIG.getProperty("password") + "@" + CONFIG.getProperty("hostname")))); - Farlan
7
对我来说,它不能工作。使用了哪个版本的selenium和chrome? - Maksym

-6
尝试这段代码:
FirefoxProfile profile = new FirefoxProfile(); 

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

WebDriver driver = new FirefoxDriver(profile);

这里我们有另外一种解决方案……它对我很有效


他只问了关于Chrome驱动程序的问题。 - GhostCKY

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接