How to Use a Proxy with Selenium in Python

Selenium is a popular browser automation library for web scraping dynamic sites. But sites can easily detect your scripts as bots and block your IP address, even when using Selenium.

The solution? Proxies.

In this guide, I'll explain step-by-step how to set up and use rotating proxies in Selenium with Python to avoid IP bans.

What is a Proxy?

A proxy acts as an intermediary server that forwards requests from clients while masking their IP address. Using a proxy in Selenium routes your automated traffic through an external server, making it appear like organic user activity to target sites.

Some key benefits of proxies with Selenium:

  • Rotate IP Addresses – Each request comes from a different proxy IP, preventing IP blocks.
  • Avoid Bot Detection – Sites see traffic coming from residential proxies, not a datacenter.
  • Mimic Human Behavior – Add random delays and mouse movements between actions.

Setting a Basic Selenium Python Proxy

To use a proxy server in Selenium, we need to launch the browser with the --proxy-server argument:

from selenium import webdriver

PROXY = "138.68.60.163:8080" 

options = webdriver.ChromeOptions()
options.add_argument('--proxy-server=%s' % PROXY)

driver = webdriver.Chrome(options=options)
driver.get("http://httpbin.org/ip")

This routes all traffic through the proxy IP, allowing us to scrape anonymously.

Authentication with Username/Password Proxies

Some proxy services like Bright Data use username/password authentication. We can handle these in Selenium using the selenium-wire plugin:

from seleniumwire import webdriver 

proxy_user = 'brightdata_user'
proxy_pass = 'proxy_password'

proxy = f'http://{proxy_user}:{proxy_pass}@192.168.1.1:1000' 

seleniumwire_options = {
   'proxy': {
        'http': proxy,
        'https': proxy
    }
}

driver = webdriver.Chrome(seleniumwire_options=seleniumwire_options)
driver.get('http://httpbin.org/ip')

This allows us to easily use authenticated Bright Data proxies within Selenium scripts.

Implementing a Rotating Proxy in Python

To prevent blocks, we need to constantly rotate proxy IPs. Here is an example proxy rotator solution with Bright Data:

import random
from seleniumwire import webdriver

brightdata_proxies = [
   'username:[email protected]:8080',  
   'username:[email protected]:8080',
   # etc...
]

proxy = random.choice(brightdata_proxies)

seleniumwire_options = {
    'proxy': {
        'http': proxy,
        'https': proxy,
        'no_proxy': 'localhost,127.0.0.1'
    }   
}

driver = webdriver.Chrome(seleniumwire_options=seleniumwire_options)
driver.get("https://www.example.com")

By randomly selecting a proxy before each request, our IP address will constantly change.

Why Free Proxies Don't Cut It

I first attempted to use free public proxies in my Selenium rotating solution. But these proxies are very unstable, often offline, and get blocked easily.

After days troubleshooting, I finally tried Bright Data's premium proxies. What a difference! Here's why Bright Data is better:

Reliable – Over 99% uptime with fallback proxies ensuring connectivity.

Fast – Proxies spread across 40+ locations deliver blazing fast speeds.

Unblocks Sites – Residential proxies mimic real users and bypass anti-bot systems.

24/7 Support – Get assistance configuring proxies from Bright Data's top-notch support.

Affordable Pricing – Only pay for the requests you use instead of a monthly fee.

For these reasons, I highly recommend using paid proxies from Bright Data if you plan to scale up any web scraping project.

Conclusion

Adding proxies in Selenium helps avoid IP blocks when scraping dynamically loaded sites. By implementing a rotating proxy solution with reliable Bright Data proxies, you can scrape data at scale while mimicking organic user traffic.

I hope this guide gave you a comprehensive overview of utilizing proxies with Selenium in Python.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *