2013-10-07 147 views
9

我可以爲Firefox設置代理服務器設置,如下所示。如何在Selenium Java中爲Chrome設置代理服務器設置

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以及..任何人都可以幫助我該怎麼做?

感謝 拉吉

回答

10

您可以嘗試使用DesiredCapabilities類,像這樣:

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

你的意思是我應該給--proxy服務器,因爲它是? – user1140680

+0

或者我應該只給這個嗎? http:// user:[email protected]:8080 – user1140680

+0

只需使用您自己的設置:'capabilities.setCapability(「chrome.switches」,Arrays.asList(「--proxy-server = http://」+ CONFIG。 getProperty(「username」)+「:」+ CONFIG.getProperty(「password」)+「@」+ CONFIG.getProperty(「hostname」)));' – Farlan

-3

試試這個代碼:

FirefoxProfile profile = new FirefoxProfile(); 

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

WebDriver driver = new FirefoxDriver(profile); 

在這裏,我們有一個更解決方案....它的工作對我來說

+0

他被問及只有Chrome驅動程序。 – GhostCKY

相關問題