6

I can't connect using proxies via Selenium Firefox WebDriver.

With this configuration, the connection is generated but NOT via the proxy but the local server.

There are two questions on this matter and this documentation, but none seem to have solved this for python3:

def selenium_connect():

    proxy = "178.20.231.218"
    proxy_port = 80
    url = "https://www.whatsmyip.org/"

    fp = webdriver.FirefoxProfile()
    # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
    fp.set_preference("network.proxy.type", 1)
    fp.set_preference("network.proxy.http",proxy)
    fp.set_preference("network.proxy.http_port",proxy_port)
    fp.update_preferences()
    driver = webdriver.Firefox(firefox_profile=fp)
    driver.get(url)

I'm using Firefox webdriver version 52.0.2 and Python 3.7 and a standard Ubuntu 16.04 Docker environment.

Rimo
  • 555
  • 1
  • 8
  • 18

2 Answers2

1

Don’t you need to set the proxy with DesiredCapabilities and not in a FirefoxProfile? Like the following.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.proxy import Proxy

proxy_to_use= "xxx.xxx.xxx.xxx"
desired_capability = webdriver.DesiredCapabilities.FIREFOX
desired_capability['proxy'] = {
    'proxyType': "manual",
    'httpProxy': proxy_to_use,
    'ftpProxy': proxy_to_use,
    'sslProxy': proxy_to_use
        }
 browser = webdriver.Firefox(capabilities=desired_capability)
 browser.get(“http://www.whatsmyip.org”)
C. Peck
  • 3,641
  • 3
  • 19
  • 36
-1

I get around this issue by iterating through proxies at the Windows level, rather than with Selenium.

By programmatically reconfiguring SSH connections via PUTTY, this creates a tunnel for your entire session. A bit more upfront setup, but much more reliable.

I use a tool like AppRobotic, which has tight Windows integration, but any good macro or RPA product should do. I use Python bindings with Selenium, but VBScript for updating the Windows config.

The main AppRobotic script written in Python can run the proxy config update script in-line on every iteration while automating other applications, such as iterating through Excel or Notepad rows and doing something in the browser, because the VBScript scripts can be considered separate "macros".

James
  • 39
  • 2