2010-05-22 75 views

回答

14

the documentation page

調整現有的Firefox配置文件

你需要改變 「network.proxy.http」 & 「network.proxy.http_port」 配置文件設置。

FirefoxProfile profile = new FirefoxProfile(); 
profile.addAdditionalPreference("network.proxy.http", "localhost"); 
profile.addAdditionalPreference("network.proxy.http_port", "3128"); 
WebDriver driver = new FirefoxDriver(profile); 
+6

這在2010年可能是正確的,但addPolicyPreference方法不再存在於FirefoxProfile中setPreference將不得不通過FirefoxProfile來完成。此外,爲了讓Firefox實際使用代理「network.proxy.type」需要設置爲1,如Praveen回答提及,或者如果其PAC爲Saik0,則設置爲2。 Dan Seibert的回答顯示,我使用DesiredCapabilities的方法來做到這一點。 – EGHM 2015-12-23 03:01:11

-4

首選項 - >高級 - >網絡 - >連接(Firefox的配置如何連接到互聯網)

+0

我知道這一點。當Selenium運行firefox窗口工作時,帶代理的連接選項卡是空的。 – Ockonal 2010-05-23 04:05:41

8

WebDriver API已更改。設置代理服務器的當前片段是

FirefoxProfile profile = new FirefoxProfile(); 
profile.setPreference("network.proxy.http", "localhost"); 
profile.setPreference("network.proxy.http_port", "3128"); 
WebDriver driver = new FirefoxDriver(profile); 
+3

代理身份驗證如何? – 2012-03-06 18:33:08

43

network.proxy.http_port值應該是整數(沒有引號應使用)和network.proxy.type應設置爲1(ProxyType.MANUAL,手動代理設置)

FirefoxProfile profile = new FirefoxProfile(); 
profile.setPreference("network.proxy.type", 1); 
profile.setPreference("network.proxy.http", "localhost"); 
profile.setPreference("network.proxy.http_port", 3128); 
WebDriver driver = new FirefoxDriver(profile); 
+5

如果有人告訴我如何在firefox個人資料中設置「user:[email protected]:2874」,將會非常感激。我試過'profile.setPreference(「network.proxy.http」,「user:[email protected]:2874」)'但顯然這是行不通的。 – Shane 2012-03-03 12:41:07

+4

代理身份驗證怎麼樣? – 2012-03-06 18:33:17

+2

@Shane:profile.setPreference(「network.proxy.http」,「http:// user:[email protected]」),然後profile.setPreference(「network.proxy.http_port」,2874)應該工作。 – learnJQueryUI 2013-05-13 01:53:43

0

有是另一種解決方案,我期待因爲這樣的代碼有問題(它在Firefox中設置系統代理):

FirefoxProfile profile = new FirefoxProfile(); 
profile.setPreference("network.proxy.http", "localhost"); 
profile.setPreference("network.proxy.http_port", "8080"); 
driver = new FirefoxDriver(profile); 

我更喜歡這個解決方案,它強制代理手動設置在Firefox中。 要做到這一點,使用org.openqa.selenium.Proxy對象設置火狐:

FirefoxProfile profile = new FirefoxProfile(); 
localhostProxy.setProxyType(Proxy.ProxyType.MANUAL); 
localhostProxy.setHttpProxy("localhost:8080"); 
profile.setProxyPreferences(localhostProxy); 
driver = new FirefoxDriver(profile); 

,如果它可以幫助...

4

對於基於PAC的URL

Proxy proxy = new Proxy(); 
proxy.setProxyType(Proxy.ProxyType.PAC); 
proxy.setProxyAutoconfigUrl("http://some-server/staging.pac"); 
DesiredCapabilities capabilities = new DesiredCapabilities(); 
capabilities.setCapability(CapabilityType.PROXY, proxy); 
return new FirefoxDriver(capabilities); 

希望這可能有所幫助。

+0

如何在此代理中進行身份驗證?我正在嘗試'http:// code:Blue%4019 @ proxypath.pac'。但我無法加載任何頁面。 – codeomnitrix 2013-06-20 09:07:54

0
FirefoxProfile profile = new FirefoxProfile(); 
String PROXY = "xx.xx.xx.xx:xx"; 
OpenQA.Selenium.Proxy proxy = new OpenQA.Selenium.Proxy(); 
proxy.HttpProxy=PROXY; 
proxy.FtpProxy=PROXY; 
proxy.SslProxy=PROXY; 
profile.SetProxyPreferences(proxy); 
FirefoxDriver driver = new FirefoxDriver(profile); 

這是C#

17

我只是有樂趣這個問題幾天的,這是我很難找到HTTPS的答案,所以這裏是我的起飛,爲Java:

FirefoxProfile profile = new FirefoxProfile(); 
    profile.setPreference("network.proxy.type", 1); 
    profile.setPreference("network.proxy.http", "proxy.domain.example.com"); 
    profile.setPreference("network.proxy.http_port", 8080); 
    profile.setPreference("network.proxy.ssl", "proxy.domain.example.com"); 
    profile.setPreference("network.proxy.ssl_port", 8080); 
    driver = new FirefoxDriver(profile); 

陷阱此處只輸入域名,而不是http://proxy.domain.example.com,屬性名是.ssl而不是.https

現在我有更多的福ñ試圖讓它接受我的自簽名的證書......

+2

謝謝,HTTPS只適用於這些設置 – AdamSkywalker 2016-05-19 15:50:56

5

在情況下,如果你有一個自動配置URL -

 FirefoxProfile firefoxProfile = new FirefoxProfile(); 
     firefoxProfile.setPreference("network.proxy.type", 2); 
     firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://www.etc.com/wpad.dat"); 
     firefoxProfile.setPreference("network.proxy.no_proxies_on", "localhost"); 
     WebDriver driver = new FirefoxDriver(firefoxProfile); 
4

下面是使用DesiredCapabilities的Java示例。我用它將硒測試泵入Jmeter。(只是在HTTP請求興趣)

import org.openqa.selenium.Proxy; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.remote.CapabilityType; 
import org.openqa.selenium.remote.DesiredCapabilities; 

String myProxy = "localhost:7777"; //example: proxy host=localhost port=7777 
DesiredCapabilities capabilities = new DesiredCapabilities(); 
capabilities.setCapability(CapabilityType.PROXY, 
          new Proxy().setHttpProxy(myProxy)); 
WebDriver webDriver = new FirefoxDriver(capabilities); 
1

火狐代理:JAVA

String PROXY = "localhost:8080"; 

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy(); 

proxy.setHttpProxy(PROXY)setFtpProxy(PROXY).setSslProxy(PROXY); 

DesiredCapabilities cap = new DesiredCapabilities(); 

cap.setCapability(CapabilityType.PROXY, proxy); 

WebDriver driver = new FirefoxDriver(cap); 
5

只需添加到上面給出的解決方案,

添加的可能性(整數值)的列表。 「network.proxy.type」。

0 - Direct connection (or) no proxy. 

1 - Manual proxy configuration 

2 - Proxy auto-configuration (PAC). 

4 - Auto-detect proxy settings. 

5 - Use system proxy settings. 

因此,根據我們的要求,「network.proxy.type」值應如下所述設定。

FirefoxProfile profile = new FirefoxProfile(); 
profile.setPreference("network.proxy.type", 1); 
WebDriver driver = new FirefoxDriver(profile);