Selenium

Selenium proxy setup

Daria avatar
Written by Daria
Updated over a week ago

Selenium is a popular tool for automating your browsers with wide support of Open Source projects.

To establish a proxy connection via Selenium you would need to download a Selenium browser driver from the official website. Choose the browser from the list below and download the driver:
- Google Chrome

Connection via login/pass authorization on Google Chrome example:

Creating a Chrome extension by including two files in an archive, named proxy.zip:

Background.js

var config = { 
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "proxy.soax.com",
port: parseInt(9000)
},
bypassList: ["soax.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "PROXY_USERNAME",
password: "PROXY_PASSWORD"
}
};
}

chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);


Manifest.json

{ 
"version": "1.0.0",
"manifest_version": 3,
"name": "Chrome Proxy",
"permissions": [
"Proxy",
"Tabs",
"unlimitedStorage",
"Storage",
"<all_urls>",
"webRequest",
"webRequestBlocking" ],
"background": {
"service_worker": "background.js"
},
"Minimum_chrome_version":"76.0.0"
}

The Chrome extension can be added to Selenium using the add_extension method:

from selenium import webdriver 
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_extension("proxy.zip")
driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)
driver.get("http://google.com")
driver.close()

In order to change the port configuration to be dynamic, please use the following command:

from selenium import webdriver from selenium.webdriver.common.proxy import Proxy, ProxyType from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import random # Import the 'random' module # Proxy credentials proxy_username = "YOUR PACKAGE LOGIN" proxy_password = "wifi;us;;;" # Proxy server address proxy_server = "proxy.soax.com" # Generate a random proxy port number between 9000 and 9999 new_proxy_port = random.randint(9000, 9999) # Configure proxy with the new port proxy = Proxy() proxy.proxy_type = ProxyType.MANUAL proxy.http_proxy = f"{proxy_server}:{new_proxy_port}" proxy.ssl_proxy = f"{proxy_server}:{new_proxy_port}" proxy.socks_proxy = f"{proxy_server}:{new_proxy_port}" proxy.ssl_proxy = f"{proxy_server}:{new_proxy_port}" proxy.ftp_proxy = f"{proxy_server}:{new_proxy_port}" proxy.no_proxy = "" # Authentication credentials auth_username = proxy_username auth_password = proxy_password

Connecting via IP authorization

from selenium import webdriver 
PROXY = "proxy.soax.com:10000"
chrome_options = WebDriver.ChromeOptions()
chrome_options.add_argument('proxy.soax.com' % PROXY) chrome = webdriver.Chrome(chrome_options=chrome_options) chrome.get("https://www.google.com")

Please feel free to reach out to support if you have any questions :)

Did this answer your question?