Guide to Implement Rotating Proxies in Selenium
Step-by-step codes to implement Rotating Proxy with Selenium in Chrome and Firefox using Python.
We'll demonstrate how to use our Rotating Proxy with Selenium in Chrome/Firefox.
If you want to learn more about rotating proxies, read our detailed guide on rotating proxies.
Proxy Authentication
Selenium doesn’t natively support username:password
authentication for a proxy. Some plugins can help, such as this one (for Node.js) or this one (for Python).
In the code, we use:
146.185.207.3:3081
as a sample proxy gateway.
PPR06ZZSY
is a demo username.
pLzCSeVmd3
is a demo password.
You should use the real one got from your client area.
We use the URL https://proxycompass.com/ip-address/ for the test. It displays the visitor's IP address. You should see a new IP each time you use our rotating proxy to access it.
Selenium Chrome Proxy
Here is some code showing how to use our rotating proxy with Selenium Chrome.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
# Proxy settings
PROXY = "146.185.207.3:3081"
PROXY_USER = "PPR06ZZSY"
PROXY_PASS = "pLzCSeVmd3"
# Set up Chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
# Create Chrome driver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
# Open proxy login page
driver.get(f"http://{PROXY_USER}:{PROXY_PASS}@{PROXY}")
# Allow some time for the proxy to authenticate
time.sleep(5)
# Visit the target page
driver.get("https://proxycompass.com/ip-address/")
body_text = driver.find_element_by_tag_name('body').text
print(body_text)
# Close the browser
driver.quit()
Selenium Firefox Proxy
Here is some code showing how to use our rotating proxy with Selenium Firefox.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.proxy import Proxy, ProxyType
from webdriver_manager.firefox import GeckoDriverManager
# Proxy settings
PROXY = "146.185.207.3:3081"
PROXY_USER = "PPR06ZZSY"
PROXY_PASS = "pLzCSeVmd3"
# Set up Firefox profile for proxy
profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.http', PROXY.split(':')[0])
profile.set_preference('network.proxy.http_port', int(PROXY.split(':')[1]))
profile.set_preference('network.proxy.ssl', PROXY.split(':')[0])
profile.set_preference('network.proxy.ssl_port', int(PROXY.split(':')[1]))
profile.set_preference('network.proxy.ftp', PROXY.split(':')[0])
profile.set_preference('network.proxy.ftp_port', int(PROXY.split(':')[1]))
profile.set_preference('network.proxy.socks', PROXY.split(':')[0])
profile.set_preference('network.proxy.socks_port', int(PROXY.split(':')[1]))
profile.set_preference('network.proxy.socks_version', 5)
# Set up proxy authentication
profile.set_preference('network.proxy.username', PROXY_USER)
profile.set_preference('network.proxy.password', PROXY_PASS)
# Initialize the driver
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()), firefox_profile=profile)
# Visit the target page
driver.get("https://proxycompass.com/ip-address/")
body_text = driver.find_element_by_tag_name('body').text
print(body_text)
# Close the browser
driver.quit()