Alisa Bom
Alisa Bom

Reputation: 61

How to use Selenium with Firefox Proxy in Selenium 4.x

I need to pass through options as profiles are deprecated. I am trying to use FF to proxy to a secure browser. However, I ran into a problem, which is described below:

My code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.proxy import Proxy, ProxyType

socks = '127.0.0.1:9050'
profile_path = '~/Library/Application\ Support/TorBrowser-Data/Browser/tjv18qgq.default'
capabilities = webdriver.DesiredCapabilities.FIREFOX
proxy = Proxy({
    "proxyType": 'manual',
    "httpProxy": socks,
    "sslProxy": socks,
    "noProxy": ''
})
proxy.add_to_capabilities(capabilities)
options = FirefoxOptions()
options.headless = False
options.set_preference('profile', profile_path)
options.set_capability("proxy", proxy)
service = Service(executable_path=GeckoDriverManager().install())
driver = webdriver.Firefox(service=service, options=options)

driver.get('https://check.torproject.org/')
element = driver.find_element(By.TAG_NAME, 'h1')
if element.text == 'Sorry. You are not using Tor.':
    print('Not connected')
    driver.close()

My output:

====== WebDriver manager ======
Current firefox version is 95.0
Get LATEST geckodriver version for 95.0 firefox
Driver [/Users//.wdm/drivers/geckodriver/macos/v0.30.0/geckodriver] found in cache
Traceback (most recent call last):
  File "/Users//dev/awesomeCRM/Backend/octopus-py/main.py", line 23, in <module>
    driver = webdriver.Firefox(service=service, options=options)
  File "/Users//dev/awesomeCRM/Backend/octopus-py/venv/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 179, in init
    RemoteWebDriver.__init__(
  File "/Users//dev/awesomeCRM/Backend/octopus-py/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 268, in init
    self.start_session(capabilities, browser_profile)
  File "/Users//dev/awesomeCRM/Backend/octopus-py/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 356, in start_session
    w3c_caps = _make_w3c_caps(capabilities)
  File "/Users//dev/awesomeCRM/Backend/octopus-py/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 102, in _make_w3c_caps
    if caps.get('proxy') and caps['proxy'].get('proxyType'):
AttributeError: 'Proxy' object has no attribute 'get'

Upvotes: 6

Views: 12230

Answers (2)

Xuemin LU
Xuemin LU

Reputation: 51

Firefox Proxy Setting for selenium==4.15.2

options = webdriver.FirefoxOptions()
options.set_preference("network.proxy.type", 1)
options.set_preference("network.proxy.http", "127.0.0.1")
options.set_preference("network.proxy.http_port", 7890)
options.set_preference('network.proxy.socks', '127.0.0.1')
options.set_preference('network.proxy.socks_port', 7890)
options.set_preference('network.proxy.socks_remote_dns', False)
options.set_preference("network.proxy.ssl", "127.0.0.1")
options.set_preference("network.proxy.ssl_port", 7890)

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193108

FirefoxProfile() have been Deprecated and with to use a custom profile you have to use an instance of Options.


The configurations which was earlier set through profile.set_preference() now can be set through options.set_preference() as follows:

from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

profile_path = r'C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\s8543x41.default-release'
options=Options()
options.set_preference('profile', profile_path)
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', '127.0.0.1')
options.set_preference('network.proxy.socks_port', 9050)
options.set_preference('network.proxy.socks_remote_dns', False)
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
driver.quit()

tl; dr

Setting a custom profile


Outro

using http proxy with selenium Geckodriver

Upvotes: 5

Related Questions