Volatil3
Volatil3

Reputation: 14978

Firefox GeckoDriver not accepting HTTP Proxy IP with user authentication

So I have the code below, it is still showing the real IP. No error produced either. Sorry can't share real proxy details :)

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.firefox.options import Options
PROXY_HOST = "206.41.127.230"

PROXY_PORT = "603230"

USERNAME = "xxx"

PASSWORD = "xx"

profile = webdriver.FirefoxProfile()

profile.set_preference("network.proxy.type", 1)

profile.set_preference("network.proxy.http", PROXY_HOST)

profile.set_preference("network.proxy.http_port", PROXY_PORT)

profile.set_preference("network.proxy.socks_username", USERNAME)

profile.set_preference("network.proxy.socks_password", PASSWORD)

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'/usr/local/bin/geckodriver', firefox_profile=profile)
driver.get('https://httpbin.org/ip')
html = driver.page_source
print(html)
driver.quit()

Upvotes: 1

Views: 2024

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193098

Seems you were close. You need to invoke update_preferences() for the FirefoxProfile instance profile and you can use the following solution:

from selenium import webdriver

PROXY_HOST = "206.41.127.230"
PROXY_PORT = "6032"
USERNAME = "xxx"
PASSWORD = "xx"

profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference("network.proxy.http", PROXY_HOST)
profile.set_preference("network.proxy.http_port", PROXY_PORT)
profile.set_preference("network.proxy.socks_username", USERNAME)
profile.set_preference("network.proxy.socks_password", PASSWORD)
profile.update_preferences()
options = webdriver.FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'/usr/local/bin/geckodriver', firefox_profile=profile)
driver.get('https://httpbin.org/ip')
html = driver.page_source
print(html)
driver.quit()

References

You can find a relevant discussions in:

Upvotes: 2

Related Questions