Python authorization of socks5 proxy in selenium for Firefox

When I use a proxy without username and password, everything works fine

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.proxy import *

profile = webdriver.FirefoxProfile() 

profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks", "ip")
profile.set_preference("network.proxy.socks_port", port)
profile.set_preference("network.proxy.socks_version", 5)
profile.update_preferences()

driver = webdriver.Firefox(firefox_profile=profile)

driver.get("http://api64.ipify.org")
print(driver.page_source)

driver.close()
driver.quit()
driver.get('http://api64.ipify.org')

However, when I try to log in with username and password

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.proxy import *

profile = webdriver.FirefoxProfile() 

profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks", "ip")
profile.set_preference("network.proxy.socks_port", port)
profile.set_preference("network.proxy.socksUsername", "username")
profile.set_preference("network.proxy.socksPassword", "password")
profile.set_preference("network.proxy.socks_version", 5)
profile.update_preferences()

driver = webdriver.Firefox(firefox_profile=profile)

driver.get("http://api64.ipify.org")
print(driver.page_source)

driver.close()
driver.quit()
driver.get('http://api64.ipify.org')

An error occurs

Traceback (most recent call last):
  File "step_one.py", line 22, in <module>
    driver.get("http://api64.ipify.org")
  File "/usr/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/usr/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=connectionFailure&u=http%3A//api64.ipify.org/&c=UTF-8&d=Firefox%20can%E2%80%99t%20establish%20a%20connection%20to%20the%20server%20at%20api64.ipify.org.

I also changed network. proxy. socksUsername to network.proxy.socks_username, - password in the same way. But the error is the same

Who knows how to solve it ? Or is there another way to log in ?

Author: More Delay, 2020-10-13

1 answers

It turns out that there is no way through selenium, but you can use selenium-wire

options = {
'proxy': {
    'http': 'socks5h://username:password@ip:port',
    'https': 'socks5h://username:password@ip:port',
    'no_proxy': 'localhost,127.0.0.1' # ,dev_server:8080
    }
}
driver = webdriver.Firefox(seleniumwire_options=options)
 0
Author: More Delay, 2020-10-14 05:02:17