I urgently need to use Selenium with Authenticated Proxy. I saw that Selenium doesn’t have anything practical to do this, just proxies without authentication, if I run the argument to register the proxy it opens a pop-up for you to enter the username and password, this doesn’t work for me because I need to open 10 pages at the same time, with varying sizes and varying positions.
I tried some solutions for this, the first was to use something called “Selenium-wire”, but it has some problems, it is VERY SLOW to run the functions, all compilers to transform it into EXE identify it as Virus and Malware and it does not complete all the processes simply stops working and only completes the process for some windows.
Second attempt was using Seleniumbase and using its Driver(), it works fine, but I can’t position the pages when they are opened and it seems that it is very slow when opening the pages.
Third attempt was using an old code that is a native form of Selenium but when I run capabilities it seems to have changed the function and it didn’t work for me.
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium import webdriver
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "username:password@ip:port"
proxy.ssl_proxy = "username:password@ip:port"
capabilities = webdriver.DesiredCapabilities.CHROME.copy()
proxy.add_to_capabilities(capabilities)
Fourth attempt is to use an extension and configure the proxy there, I managed it and it is working very well! But some windows are not opening in the language I need, I need it to open in Portuguese, but it opens mixed, sometimes in Portuguese and sometimes in English, it was the best solution so far, but my users are all Brazilians who speak Portuguese, I need the windows to open in Portuguese!
def create_chromedriver(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS, USER_AGENT, pos_x, pos_y, altura, largura, posicao):
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']
);
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === 'Accept-Language') {
details.requestHeaders[i].value = 'pt-BR,pt;q=0.9';
break;
}
}
return {requestHeaders: details.requestHeaders};
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]
);
""" % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)
def get_chromedriver(use_proxy=True, user_agent=USER_AGENT, x=pos_x, y=pos_y, alt=altura, larg=largura, posicao_janela=posicao):
path = os.path.dirname(os.path.abspath(__file__))
chrome_options = Options()
if use_proxy:
pluginfile = f'proxy_{posicao_janela}.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)
arguments = ['--disable-notifications', '--disable-popup-blocking', f"--window-position={x},{y}", f"--window-size={larg},{alt}"]
for argument in arguments:
chrome_options.add_argument(argument)
driver = webdriver.Chrome(options=chrome_options)
return driver
driver = get_chromedriver(use_proxy=True)
return driver
Felipe Ataide is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.