python-3.xseleniumproxyselenium-chromedriverproxy-authentication

Python selenium chromedriver authenticate luminati proxy?


Hi I am using selenium chromedriver and using luminati proxy with it. But I cant figure out how to set proxy username and password.

I have tried this code:

import random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.proxy import Proxy, ProxyType, ProxyTypeFactory
from selenium.webdriver.support import expected_conditions as EC

username = 'lum-customer-XXXX-static'
password = 'XXXXXX'
port = 22225
session_id = random.random()
super_proxy_url = ('http://%s-session-%s:%s@zproxy.lum-superproxy.io:%d' %(username, session_id, password, port))

proxy = Proxy()
proxy.http_proxy = proxy_url
proxy.ftp_proxy = proxy_url
proxy.ssl_proxy = proxy_url
proxy.no_proxy = "localhost"
proxy.proxy_type = ProxyType.MANUAL

chromedriver_path = '/usr/local/share/chromedriver' # Change this to your own chromedriver path!
driver = webdriver.Chrome(executable_path=chromedriver_path,chrome_options=chrome_options)
driver.get("https://whatismyipaddress.com/")

But Is not connecting to my the proxy server. I have also tried desired_capabilities. Its code is shown as below.

desired_capabilities = webdriver.DesiredCapabilities.CHROME
desired_capabilities['proxy'] = {
    "proxyType":"manual",
    "httpProxy":"zproxy.lum-superproxy.io:22225",
    "ftpProxy":"zproxy.lum-superproxy.io:22225",
    "sslProxy":"zproxy.lum-superproxy.io:22225",
    "socksUsername":"lum-customer-XXXXX-zone-static",
    "socksPassword":"XXXX",
    "socksVersion": 4
}
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,desired_capabilities=desired_capabilities)
driver.get("https://whatismyipaddress.com/")
WebDriverWait(driver, 20).until(EC.alert_is_present())
alert = driver.switch_to_alert()
alert.send_keys(username + Keys.TAB + password)
# time.sleep(5)
alert.accept()

But the username and password is not filling in the alert box that requires username and password for the proxy.

Please help me. If there is any alternate way to authenticate the proxy then please share with me. Thanks


Solution

  • Using proxy authentication with selenium can be tricky because you need to use the right versions of webdriver, selenium and browser to all support using a proxy. My recommendation is to use one of these alternatives to setting the proxy directly in your code using selenium.

    1. Set the proxy with credentials on the host machine - just be careful to turn the proxy off when you don't need it or to set it up in a virtual machine that you only use with the proxy. that is how they recommend doing it on the selenium website: https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#using-a-proxy
    2. Use Luminati Proxy manager (https://github.com/luminati-io/luminati-proxy) to set up a proxy port that does not require authentication and passes the traffic on with your proxy credentials.
    3. Use puppeteer to handle headless chrome (https://pypi.org/project/pyppeteer/) as the puppeteer library comes with the right version of chromium and is much easier to get started with.