3
votes
System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
    Proxy p = new Proxy();
    p.setSocksProxy("83.209.94.87:35923");
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(CapabilityType.PROXY, p);
    WebDriver driver = new FirefoxDriver(cap);
    driver.get("https://www.google.com.au");

This code is inside the main method. When I run this code, firefox is launched but the google url isn't followed and the proxy is not set to the one I specify in the code above. How can I fix this?

public static void main(String[] args) throws InterruptedException, IOException, UnsupportedEncodingException {
    while (true) {
    System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
    WebDriver driver;
    String PROXY = "83.209.94.87:35923";
      //Bellow given syntaxes will set browser proxy settings using DesiredCapabilities.
      Proxy proxy = new Proxy();
      proxy.setAutodetect(false);
      proxy.setProxyType(Proxy.ProxyType.MANUAL);
      proxy.setSocksProxy(PROXY);
      DesiredCapabilities cap = new DesiredCapabilities();
      cap.setCapability(CapabilityType.PROXY, proxy);
      //Use Capabilities when launch browser driver Instance.
      driver = new FirefoxDriver(cap);`
3
Add this proxy.setProxyType(Proxy.ProxyType.MANUAL); and proxy.setAutodetect(false); after Proxy p = new Proxy(); - Tarun Lalwani
I tried doing that but when my program launches firefox, the proxy setting is set to 'Use system proxy settings'. Check post for new code. - user8497118
Check post again - user8497118
There is a open bug which prevents you from using proxies as of now github.com/mozilla/geckodriver/issues/764 - Tarun Lalwani

3 Answers

3
votes

Because a bug you cannot use the Proxy object as of now. You should use the below code

    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.socks", "83.209.94.87");
    profile.setPreference("network.proxy.socks_port", 35923);

    FirefoxDriver driver = new FirefoxDriver(profile);
    driver.get("https://www.ipinfo.io");

The bug is discussed on https://github.com/mozilla/geckodriver/issues/764 and you see what the Marionette driver do in background on below link

https://dxr.mozilla.org/mozilla-central/source/testing/marionette/session.js#155

So above code just replicates the same

1
votes

Work in Selenium 3.14.2, Firefox 62, C# .NET 4.5

FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"GeckoDriver19", "geckodriver.exe");
service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";

FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.SetPreference("network.proxy.type", 1);
firefoxOptions.SetPreference("network.proxy.socks", "127.0.0.1");
firefoxOptions.SetPreference("network.proxy.socks_port", 1080);

IWebDriver driver = new FirefoxDriver(service, firefoxOptions);

driver.Navigate().GoToUrl("https://www.hbus.com/register");
0
votes

you can set proxy details with credentials here in your automation code as above answers explained but another way to do it without sharing your details in your java or python code with firefox profile.
firefox provides profiles, we can create profile for every user and customize it for proxy ,bookmarks etc.
windows user : open run(win+R) and type 'firefox -p'
linux user : run command 'firefox -p'
1- it will open a dialog box where you can create your profile then select that profile and open firefox.
2- open a new tab and search 'about:config' .accept the Risk and Continue then click on show all
3-here you can search and set all the property
example:
network.proxy.type   1
1 for Manually
2 for Automatic proxy
for Manuall proxy -
property                     value
network.proxy.http              your proxy ip
network.proxy.http_port               port number
network.proxy.ssl               your proxy ip
network.proxy.ssl_port               port number
network.proxy.ftp               your proxy ip
network.proxy.ftp_port               port number

(to find your profile name )
Linux : cd .mozilla/firefox/
windows: Press. +R on the keyboard. A Run dialog will open. Type in: %APPDATA%\Mozilla\Firefox\Profiles\ Click OK. A window will open containing profile folders now load this profile in java code

FirefoxOptions options = new FirefoxOptions();
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
FirefoxProfile profile=new FirefoxProfile(new File("path of your profile"));
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);
System.setProperty("webdriver.gecko.driver", "path of gecko driver");
driver.get("url");