Selenium Proxy Setup in Python: Rotation and Authentication Guide
Introduction
When running web scraping or automation tasks at scale, using proxies is crucial. Proxies help mask your IP address, handle geolocation restrictions, and avoid getting blocked by websites. Selenium, a popular tool for automating browsers, supports proxy configurations directly, allowing developers to set up and manage proxies easily.
In this guide, we'll walk through the process of setting up proxies in Selenium using Python, including handling proxy rotation and authentication if needed. We'll cover both Chrome and Firefox browsers, as they are the most commonly used in automation tasks.
Setting Up Proxies with Selenium
Prerequisites
Before you start, ensure you have the following:
- Python Installed: Make sure Python is installed on your machine.
- Selenium Library: Install Selenium using pip:
`bash
pip install selenium
`
- WebDriver for Chrome/Firefox: Download and set up the appropriate WebDriver for your browser (ChromeDriver for Chrome, geckodriver for Firefox).
Basic Proxy Setup
#### Using Chrome
To configure a proxy in Selenium with Chrome, you need to use ChromeOptions.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
# Set up proxy
proxy = "http://your.proxy.server:port"
chrome_options = Options()
chrome_options.add_argument(f"--proxy-server={proxy}")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.get("https://www.whatismyip.com/")
#### Using Firefox
For Firefox, you use FirefoxProfile.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager
# Set up proxy
proxy = "http://your.proxy.server:port"
firefox_options = Options()
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("network.proxy.type", 1)
firefox_profile.set_preference("network.proxy.http", "your.proxy.server")
firefox_profile.set_preference("network.proxy.http_port", port)
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()), options=firefox_options, firefox_profile=firefox_profile)
driver.get("https://www.whatismyip.com/")
Proxy Authentication
Proxies that require authentication can be a bit more complex. You need to use an extension or modify the proxy settings accordingly.
#### Using Chrome with Auth
For authenticated proxies in Chrome, you might need to use a browser extension like Proxy SwitchyOmega. Here's how you can set it up programmatically:
- Download and Install Proxy Auto-Config (PAC) Extension.
- Modify your code to load the extension.
import os
proxy_extension_path = "/path/to/your/proxy-extension.crx"
chrome_options.add_argument(f"--load-extension={proxy_extension_path}")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
#### Using Firefox with Auth
Firefox can handle proxy authentication through the profile settings:
firefox_profile.set_preference("network.proxy.http", "your.proxy.server")
firefox_profile.set_preference("network.proxy.http_port", port)
firefox_profile.set_preference("network.proxy.share_proxy_settings", True)
firefox_profile.set_preference("network.ntlm.send_http_in_response_to_401", False)
firefox_profile.set_preference("network.auth.use-native-ui", False)
# Add credentials
firefox_profile.set_preference("network.http.phishy-user-evade.enabled", False)
firefox_profile.set_preference("network.proxy.share_proxy_settings", True)
firefox_profile.set_preference("network.automatic-ntlm-auth.trusted-sites", ".yourdomain.com")
Proxy Rotation
Proxy rotation is essential when you need to avoid getting blocked by websites. Here’s how you can implement a simple proxy rotation loop.
Rotating Proxies in Selenium
Let's assume you have a list of proxies:
proxies = [
"http://proxy1:port",
"http://proxy2:port",
"http://proxy3:port"
]
def rotate_proxy(driver, proxies):
for proxy in proxies:
print(f"Using proxy: {proxy}")
driver.execute_cdp_cmd("Network.setUserAgentOverride", {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
})
driver.execute_cdp_cmd("Network.enable", {})
driver.get(f"--proxy-server={proxy}")
# Perform actions with the current proxy
print("Proxy rotation complete")
# Example usage
rotate_proxy(driver, proxies)
Conclusion
Setting up and managing proxies in Selenium can significantly enhance your automation tasks. Whether you're dealing with basic proxy configurations or more complex scenarios like authentication and rotation, Selenium provides the tools to handle them efficiently.
Remember, maintaining a reliable list of working proxies is crucial. If you need an automated solution that tests and rotates proxies locally on your machine, ProxyForge offers a one-off $29 unlock with no subscription:
https://proxyforge.dev/#download
ProxyForge ensures that all proxies are verified on your machine before they're used in your automation tasks, saving you time and effort.
With this guide, you should be well-equipped to manage proxies in Selenium for your web scraping and automation needs. Happy coding!
Free proxies break. ProxyForge doesn't.
The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.
Download ProxyForge free →