Last modified: Dec 05, 2023 By Alexander Williams

Python Selenium: Using Proxies - Examples

Example 1: Configure Proxy with Selenium WebDriver


from selenium import webdriver

# Define proxy settings
proxy_address = "http://your-proxy-address:port"

# Create a Proxy object
proxy = {
    "http": proxy_address,
    "https": proxy_address,
}

# Create a ChromeOptions object
chrome_options = webdriver.ChromeOptions()

# Add the proxy settings to ChromeOptions
chrome_options.add_argument(f'--proxy-server={proxy_address}')

# Create a Chrome driver with the configured ChromeOptions
driver = webdriver.Chrome(options=chrome_options)

# Navigate to a webpage to test the proxy
driver.get('https://example.com')

# Continue with your automation script...

Output: N/A

Example 2: Use a Proxy Rotation Service


from selenium import webdriver

# Use a proxy rotation service that provides a rotating pool of proxies
# Update 'your-rotation-service-url' with the actual URL of the rotation service
rotation_service_url = "http://your-rotation-service-url"

# Create a ChromeOptions object
chrome_options = webdriver.ChromeOptions()

# Add the proxy rotation service extension to ChromeOptions
chrome_options.add_extension('path/to/proxy-rotation-extension.crx')

# Specify the rotation service URL as an argument
chrome_options.add_argument(f'--proxy-server={rotation_service_url}')

# Create a Chrome driver with the configured ChromeOptions
driver = webdriver.Chrome(options=chrome_options)

# Navigate to a webpage to test the rotating proxies
driver.get('https://example.com')

# Continue with your automation script...

Output: N/A