Webdriver和Firefox代理服务器

44

有没有办法设置Firefox的代理设置?我在这里找到了关于FoxyProxy的信息,但当Selenium工作时,窗口中的插件未被激活。

有没有办法设置Firefox的代理设置?我在这里找到了关于FoxyProxy的信息,但当Selenium工作时,窗口中的插件未被激活。

13个回答

53

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);

8
如果有人能告诉我如何在Firefox配置文件中设置"user:pass@1.1.1.1:2874",我将不胜感激。我尝试了profile.setPreference("network.proxy.http", "user:pass@1.1.1.1:2874")但显然它不起作用。 - Shane
5
代理认证怎么处理? - coding_idiot
3
@Shane: 使用 profile.setPreference("network.proxy.http", "http://user:pass@1.1.1.1") ,然后使用 profile.setPreference("network.proxy.http_port", 2874) 应该是有效的。 - Harry
@Arya:嗯,事实上我发现webdriver在我当时尝试做的大量工作中太笨重了,所以我转向使用requests(一个Python库)进行纯HTTP/HTTPS请求来完成我的工作。 - Shane

24

我刚刚花了几天时间解决这个问题,对于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

现在,我正在尝试让它接受我的自签名证书,这真是太有趣了...


你有没有成功使用自签名证书? :) - Nick Grealy

22

请查看文档页面

调整现有的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);

11
这个说法可能在2010年是正确的,但是addAdditionalPreference方法在FirefoxProfile上已经不存在了,必须使用setPreference来通过FirefoxProfile进行设置。此外,为了让Firefox实际使用代理,“network.proxy.type”需要被设置为1,就像Praveen的回答中提到的那样,或者如果是PAC,则设置为2,就像Saik0所示。我使用DesiredCapabilities的方式来完成这个操作,就像Dan Seibert的回答所展示的那样。 - EGHM

12

仅补充上述解决方案,

添加“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);

你的信息来源是什么?你怎么知道network.proxy.type的可能取值列表是什么? - robertspierre

9
WebDriver API已经更改。设置代理的当前代码片段为:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", "3128");
WebDriver driver = new FirefoxDriver(profile);

7
代理服务器认证怎么办? - coding_idiot

5

以下是使用DesiredCapabilities的Java示例。我将其用于将Selenium测试注入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); 

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

针对基于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);

我希望这可以帮助您。


如何在此代理服务器中进行身份验证?我尝试使用http://code:Blue%4019@proxypath.pac,但无法加载任何页面。 - codeomnitrix

4
根据最新文档,涉及到IT技术相关内容。
from selenium import webdriver

PROXY = "<HOST:PORT>"
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
    "httpProxy": PROXY,
    "ftpProxy": PROXY,
    "sslProxy": PROXY,
    "proxyType": "MANUAL",

}

with webdriver.Firefox() as driver:
    # Open URL
    driver.get("https://selenium.dev")

2

火狐代理: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);

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