38

are there any ways to set firefox's proxy settings? I found here information about FoxyProxy but when Selenium works, plugins are unactivated in window.

Ripon Al Wasim
  • 34,088
  • 37
  • 146
  • 165
Max Frai
  • 52,556
  • 73
  • 182
  • 293

13 Answers13

51

Value for network.proxy.http_port should be integer (no quotes should be used) and network.proxy.type should be set as 1 (ProxyType.MANUAL, Manual proxy settings)

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);
Charles Duffy
  • 235,655
  • 34
  • 305
  • 356
Benjan Tom
  • 511
  • 1
  • 4
  • 2
  • 6
    If anybody would tell me how do I set "user:pass@1.1.1.1:2874" in firefox profile it would be much appreciated. I've tried `profile.setPreference("network.proxy.http", "user:pass@1.1.1.1:2874")` but obviously it does NOT work. – Shane Mar 03 '12 at 12:41
  • 4
    what about proxy authentication? – coding_idiot Mar 06 '12 at 18:33
  • 2
    @Shane: profile.setPreference("network.proxy.http", "http://user:pass@1.1.1.1") , then profile.setPreference("network.proxy.http_port", 2874) should work. – Harry May 13 '13 at 01:53
  • @Arya: Well, I actually found `webdriver` too clumsy for lots of work I was trying to do back then, so I turned to `requests`(a python lib) for pure `HTTP/HTTPS` requests to finish my job. – Shane Oct 07 '16 at 06:44
22

I just had fun with this issue for a couple of days and it was hard for me to find an answer for HTTPS, so here's my take, for 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);

Gotchas here: enter just the domain and not http://proxy.domain.example.com, the property name is .ssl and not .https

I'm now having even more fun trying to get it to accept my self signed certificates...

Victor
  • 9,050
  • 3
  • 23
  • 37
18

Look at the documentation page.

Tweaking an existing Firefox profile

You need to change "network.proxy.http" & "network.proxy.http_port" profile settings.

FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("network.proxy.http", "localhost");
profile.addAdditionalPreference("network.proxy.http_port", "3128");
WebDriver driver = new FirefoxDriver(profile);
RichardTowers
  • 4,394
  • 1
  • 23
  • 42
SSH
  • 2,463
  • 14
  • 21
  • 10
    This might have been correct in 2010, but the addAdditionalPreference method no longer exists on FirefoxProfile setPreference would have to be used to do this via FirefoxProfile. In addition in order to have firefox actually use the proxy "network.proxy.type" needs to be set to 1 as Praveen answer mentions or 2 if its a PAC as Saik0 shows. I use the DesiredCapabilities way of doing this as Dan Seibert's answer shows. – EGHM Dec 23 '15 at 03:01
10

Just to add to the above given solutions.,

Adding the list of possibilities (integer values) for the "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. 

So, Based on our requirement, the "network.proxy.type" value should be set as mentioned below.

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
WebDriver driver = new FirefoxDriver(profile);
Praveen
  • 1,199
  • 1
  • 10
  • 20
8

The WebDriver API has been changed. The current snippet for setting the proxy is

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", "3128");
WebDriver driver = new FirefoxDriver(profile);
Stefan Birkner
  • 21,766
  • 10
  • 52
  • 68
5

In case if you have an autoconfig 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);
Saikat
  • 8,190
  • 12
  • 69
  • 94
5

Here's a java example using DesiredCapabilities. I used it for pumping selenium tests into jmeter. (was only interested in HTTP requests)

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); 
Julián Urbano
  • 8,100
  • 1
  • 28
  • 51
Dan Seibert
  • 310
  • 2
  • 4
4

For PAC based urls

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

I hope this could help.

Antony
  • 1,345
  • 1
  • 17
  • 27
  • How to authenticate in this proxy?? I am trying as `http://code:Blue%4019@proxypath.pac`. But I am unable to load any page. – codeomnitrix Jun 20 '13 at 09:07
2

Firefox Proxy: 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);
Nick
  • 464
  • 4
  • 17
2

According to the latest documentation

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")
Harshit Rastogi
  • 144
  • 1
  • 4
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);

It is for C#

Perception
  • 75,573
  • 19
  • 170
  • 185
zhouyu
  • 41
  • 2
0

There is another solution, i looked for because a had problems with code like this (it s set the system proxy in firefox):

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

I prefer this solution, it force the proxy manual setting in firefox. To do that, use the org.openqa.selenium.Proxy object to setup Firefox :

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

if it could help...

-4

Preferences -> Advanced -> Network -> Connection (Configure how Firefox connects to the Internet)

David Baron
  • 910
  • 4
  • 5