Home > Mobile >  How to use a socks5 proxy with authentication via selenium 4, Python and Firefox (options)
How to use a socks5 proxy with authentication via selenium 4, Python and Firefox (options)

Time:12-20

I searched a lot, this came up many times over the past decade, but nothing addressing the problem or with the latest version of Selenium Python (that I can find)

If I set up a socks5 proxy server (using Ubuntu 22.04 and Dante), I can forward my requests through it using Selenium 4 and Firefox Options like so:

options = webdriver.FirefoxOptions()
PROXYaddr = 'XXX.XXX.XXX.XXX:YYYY'
PROXY_IP, PROXY_PORT = PROXYaddr.split(':')

options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', PROXY_IP)
options.set_preference('network.proxy.socks_port', int(PROXY_PORT))

driver = webdriver.Firefox(options=options)

Now this works. The problem comes in if the socks5 proxy requires a username and password authentication.

How can I add username and password auth for this socks5 proxy in Selenium 4 with Firefox?

I am looking for a solution that does not require installing extra extensions, or working with a UI as this code runs in headless mode on a server.

Thank you

CodePudding user response:

It looks like Firefox does not accept user/password auth for socks5 proxy at this time, see: https://github.com/mozilla/geckodriver/issues/1872

The bug/feature is being tracked here: https://bugzilla.mozilla.org/show_bug.cgi?id=1395886

Selenium is aware of this issue here: https://github.com/SeleniumHQ/selenium/issues/7911

For now the solution I've used as an alternative is to use no authentication on my proxy and then use ufw on the server to block all IPs except for the designated ones.

  • Related