How to Set Proxy Authentication (User & Password) Using Python + Selenium

how to set proxy with authentication in selenium chromedriver python?

Selenium Chrome Proxy Authentication

Setting chromedriver proxy with Selenium using Python

If you need to use a proxy with python and Selenium library with chromedriver you usually use the following code (Without any username and password:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % hostname + ":" + port)
driver = webdriver.Chrome(chrome_options=chrome_options)

It works fine unless proxy requires authentication. if the proxy requires you to log in with a username and password it will not work. In this case, you have to use more tricky solution that is explained below. By the way, if you whitelist your server IP address from the proxy provider or server it should not ask proxy credentials.

HTTP Proxy Authentication with Chromedriver in Selenium

To set up proxy authentication we will generate a special file and upload it to chromedriver dynamically using the following code below. This code configures selenium with chromedriver to use HTTP proxy that requires authentication with user/password pair.

import os
import zipfile

from selenium import webdriver

PROXY_HOST = '192.168.3.2' # rotating proxy or host
PROXY_PORT = 8080 # port
PROXY_USER = 'proxy-user' # username
PROXY_PASS = 'proxy-password' # 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_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)


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

def main():
driver = get_chromedriver(use_proxy=True)
#driver.get('https://www.google.com/search?q=my+ip+address')
driver.get('https://httpbin.org/ip')

if __name__ == '__main__':
main()

Function get_chromedriver returns configured selenium webdriver that you can use in your application. This code is tested and works just fine.

Read more about onAuthRequired event in Chrome.

How to authenticate proxy with username and password in Selenium using Python

You can achieve this by using AutoIt. And it has Python binding PyAutoIt. Once you installed PyAutoIt using PIP - pip install PyAutoIt, the following code does your job.

import autoit

autoit.win_wait_active("Authentication Required") # title of the dialog box to wait. so it will wait for the Authentication Required dialog
autoit.send("username", 1) # second parameter is the mode (changes how "keys" is processed)
autoit.send("{TAB}") # press tab key to go to the password field
autoit.send("password", 1)
autoit.send("{Enter}") # press enter key

For more information about the second parameter in the send method, here is the code,

def send(send_text, mode=0):
"""
Sends simulated keystrokes to the active window.
:param send_text:
:param mode: Changes how "keys" is processed:
flag = 0 (default), Text contains special characters like + and ! to
indicate SHIFT and ALT key presses.
flag = 1, keys are sent raw.
:return:
"""
AUTO_IT.AU3_Send(LPCWSTR(send_text), INT(mode))

Python selenium - Proxy connection with host, port, username, password

Dup of: How to set proxy AUTHENTICATION username:password using Python/Selenium

Selenium-wire: https://github.com/wkeeling/selenium-wire

Install selenium-wire

pip install selenium-wire

Import it

from seleniumwire import webdriver

Auth to proxy

options = {
'proxy': {
'http': 'http://username:password@host:port',
'https': 'https://username:password@host:port',
'no_proxy': 'localhost,127.0.0.1,dev_server:8080'
}
}
driver = webdriver.Firefox(seleniumwire_options=options)

Warning

Take a look to the selenium-wire cache folder. I had a problem because it take all my disk space. You have to remove it sometimes in your script when you want.

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