Here is the example of how to use our service with Selenium and Chromedriver Proxy authentication (works with recent Chrome versions with Manifest v3 required):
import os
import zipfile
import time
from selenium import webdriver
PROXY_HOST = "x.botproxy.net" # rotating web scraping proxy
PROXY_PORT = 8080
PROXY_USER = "user"
PROXY_PASS = "pass"
manifest_json = """
{
"name": "Chrome Proxy",
"description": "Use proxy with auth",
"version": "1.0.0",
"manifest_version": 3,
"permissions": [
"proxy",
"storage",
"scripting",
"tabs",
"unlimitedStorage",
"webRequest",
"webRequestAuthProvider"
],
"host_permissions": [
"<all_urls>"
],
"background": {
"service_worker": "background.js"
},
"action": {
"default_title": "Proxy Extension"
}
}
"""
background_js = """
chrome.runtime.onInstalled.addListener(() => {
const config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%s",
port: parseInt(%s)
},
bypassList: ["localhost"]
}
};
chrome.proxy.settings.set(
{value: config, scope: "regular"},
() => {}
);
});
chrome.webRequest.onAuthRequired.addListener(
function(details) {
return {
authCredentials: {
username: "%s",
password: "%s"
}
};
},
{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)
chrome_options.add_argument(
"--proxy-server=http://%s:%s" % (PROXY_HOST, PROXY_PORT)
)
if user_agent:
chrome_options.add_argument("--user-agent=%s" % user_agent)
driver = webdriver.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")
time.sleep(10)
ip = driver.find_element("xpath", "//pre").text
print("IP Address:", ip)
if __name__ == "__main__":
main()
For full explanation of the code please see this how-to