Firefox selenium gets stuck when using proxy with Python

This is my code, however when I start running it, Firefox opens up but doesn’t navigate to the site. If I try to manually enter the URL, it still doesn’t show that the proxy’s IP address.

from selenium import webdriver

firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities["marionette"] = True

PROXY = "40.91.94.165:3128"

firefox_capabilities["proxy"] = {
    "proxyType": "MANUAL",
    "httpProxy": PROXY,
    "ftpProxy": PROXY,
    "sslProxy": PROXY,
}


driver = webdriver.Firefox(capabilities=firefox_capabilities)
driver = webdriver.Firefox()

driver.get("https://wtfismyip.com")

This is the error I get when I try to stop it

^CTraceback (most recent call last):
  File "/Users/****/Developer/python/proxy.py", line 16, in <module>
    driver = webdriver.Firefox(capabilities=firefox_capabilities)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 397, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/request.py", line 79, in request
    return self.request_encode_body(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/request.py", line 171, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/poolmanager.py", line 330, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 665, in urlopen
    httplib_response = self._make_request(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 421, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 416, in _make_request
    httplib_response = conn.getresponse()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1347, in getresponse
    response.begin()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
    return self._sock.recv_into(b)

Answer

My solution is with a different approach than yours. I’m using Chrome when I want to do something similar, but I tried with Firefox in order to help you.

So my solution work with 2 more packages that you need to install. The first one is webdriver_manager and you can install it like this(make sure to install it exactly as I have it here): pip install -U webdriver-manager The second is the selenium-wire and you can install it like this pip install selenium-wire

from seleniumwire import webdriver
from webdriver_manager.firefox import GeckoDriverManager

PROXY = "40.91.94.165:3128"

proxy = {
         'proxy': {
             'http': "http://" + f"{PROXY}",
             'https': "http://" + f"{PROXY}"
            }
        }
options = webdriver.FirefoxOptions()
options.headless = False

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), 
                            options=options, seleniumwire_options=proxy)
driver.get("https://wtfismyip.com")