How to Use Authenticated Proxy in Selenium Chromedriver

Selenium proxy with authentication

you can't because you need a GUI to handle it with selenium in your case
so I would recommend using a virtual display like Xvfb display server

You can use PyVirtualDisplay (a Python wrapper for Xvfb) to run headless.

for Linux

sudo apt-get install firefox xvfb

install virtual display for python

pip install pyvirtualdisplay

then

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

# now Firefox will run in a virtual display.
# you will not see the browser.
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()

display.stop()

in this case, you do not need to add options.add_argument("--headless") argument and you can follow the answers commented above as a solution or doing it your way but I think this is the best solution for using pure selenium for proxy

Setting chromedriver proxy auth with Selenium using Python

Use DesiredCapabilities. I have been successfully using proxy authentication with the following:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

proxy = {'address': '123.123.123.123:2345',
'username': 'johnsmith123',
'password': 'iliketurtles'}


capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {'proxyType': 'MANUAL',
'httpProxy': proxy['address'],
'ftpProxy': proxy['address'],
'sslProxy': proxy['address'],
'noProxy': '',
'class': "org.openqa.selenium.Proxy",
'autodetect': False}

capabilities['proxy']['socksUsername'] = proxy['username']
capabilities['proxy']['socksPassword'] = proxy['password']

driver = webdriver.Chrome(executable_path=[path to your chromedriver], desired_capabilities=capabilities)

EDIT: it unfortunately seems this method no longer works since one of the updated to either Selenium or Chrome since this post. as of now, i do not know another solution, but i will experiment and update this if i find anything out.

How to login in a proxy server with authentication with selenium chrome driver Python 3?

Got it working like this, kindly note parts of this code are not in Python,simply copy and paste, also make sure that chromedriver.exe are in the same directory.

from selenium import webdriver
import os
import zipfile
url = 'https://whatismyipaddress.com/'
PROXY = 'XXX.XX.XX.XX' # YOUR PROXY IP ADDRESS
port = 'XXXX' # YOUR PORT NUMBER
user = 'XXXXX' # YOUR USER NAME
passw = 'XXXXX' # YOUR PASSWORD
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
background_js = """
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%s",
port: parseInt(%s)
},
bypassList: ["localhost"]
}
};

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
return {
authCredentials: {
username: "%s",
password: "%s"
}
};
}

chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
""" % (PROXY, port, user, passw)
def get_chromedriver(use_proxy=False, user_agent=None):
path = os.path.dirname(os.path.abspath(__file__))
chrome_options = webdriver.ChromeOptions()
if use_proxy:
pluginfile = 'proxy_auth_plugin.zip'

with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
chrome_options.add_extension(pluginfile)
if user_agent:
chrome_options.add_argument('--user-agent=%s' % user_agent)
driver = webdriver.Chrome(
os.path.join(path, 'chromedriver'),
chrome_options=chrome_options)
return driver


driver = get_chromedriver(use_proxy=True)
driver.get(url)

Simply copy and paste and change with your credential.
Make sure NOT to change any indentation.
Cheers



Related Topics



Leave a reply



Submit