How to Use Proxies With SeleniumBase

Set up SeleniumBase with authenticated proxies to bypass restrictions and enhance your web scraping success.
8 min read
How to Use Proxies With SeleniumBase blog image

SeleniumBase is designed as a wrapper for running Selenium instances in a testing environment. That said, it’s far more than just a wrapper. SeleniumBase allows us to run Selenium using an authenticated proxy.

Inherent Proxy Problems With Selenium

Selenium doesn’t have very good proxy support. While you can pass a --proxy-server argument into Selenium, there is no support for authenticated proxies. It gets worse though. For years, SeleniumWire was the go to option when using proxies with Selenium. Integration was incredibly easy. Sadly, SeleniumWire was deprecated over a year ago and hasn’t received any updates in over two years.

SeleniumWire Deprecation

Getting Started

To get started, we need to install SeleniumBase and write a test case. The test case is what actually controls Selenium and runs our webdriver instance for us.

Installation

pip install seleniumbase

Writing a Test Case

Here’s a simple test case. The code below makes a request to the IPinfo API. Once we receive our JSON response, we parse it and print its contents to the console.

from seleniumbase import BaseCase
from selenium.webdriver.common.by import By
import json

class ProxyTest(BaseCase):
   def test_proxy(self):
        #go to the site
        self.driver.get("https://ipinfo.io/json")
      
        #load the json response
        location_info = json.loads(self.driver.find_element(By.TAG_NAME, "body").text)

        #iterate through the dict and print its contents
        for k,v in location_info.items():
            print(f"{k}: {v}")

Invoking The Test

In order to actually run the code, we need to run the test. Instead of using python name_of_your_script.py, we’re actually going to use pytest.

Without a Proxy

If you wish to run your test script without a proxy, you can use the command below.

pytest proxy_test.py -s

You’ll receive a response similar to this.

=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item                                                                                                          

proxy_test.py ip: 23.28.108.255
hostname: d28-23-255-108.dim.wideopenwest.com
city: Westland
region: Michigan
country: US
loc: 42.3242,-83.4002
org: AS12083 WideOpenWest Finance LLC
postal: 48185
timezone: America/Detroit
readme: https://ipinfo.io/missingauth
.

==================================================== 1 passed in 1.01s ====================================================

Proxy Configuration

To use a proxy, we simply need to use the --proxy flag followed by our proxy url. Look at the format below.

--proxy=your_proxy_url:your_proxy_port

Free Proxy

Here’s an example using a free proxy. The IP address is 155.54.239.64 and we’re talking to it on port 80.

--proxy=155.54.239.64:80

Authenticated Proxy

Authenticated proxies are handled the same way. With an authenticated proxy, you simply need to include your username and password in the url.

proxy=<YOUR_USERNAME>:<YOUR_PASSWORD>@<PROXY_URL>:<PROXY_PORT>

Best Authenticated Proxy Types and Providers

When using authenticated proxies with Selenium, the best options are residential proxies, datacenter proxies, and ISP proxies, each offering different levels of anonymity and reliability. Residential proxies provide the highest success rates by using real user IPs, making them ideal for bypassing bot detection. Datacenter proxies are faster and more cost-effective but easier to detect. ISP proxies combine the benefits of both, offering speed with high trust levels.

Recommended Providers:

  • Bright Data – Industry-leading residential, ISP, and datacenter proxies with built-in session control and geo-targeting.
  • Oxylabs – Offers residential and datacenter proxies with strong global coverage.
  • Smartproxy – Affordable residential proxies with easy integration.
  • SOAX Rotating proxies with flexible pricing.

For SeleniumBase scraping, Bright Data’s residential proxies are the most reliable option, ensuring high success rates and minimal blocks.

Running With a Proxy

The example below is configured to run with one of our proxies here at Bright Data. Make sure to replace the username, zone name, and password with your own.

pytest proxy_test.py --proxy=brd-customer-<YOUR-USERNAME>-zone-<YOUR-ZONE-NAME>:<YOUR-PASSWORD>@brd.superproxy.io:33335 -s

When we run it we get the following output. If you look closely, you’ll see that our location has changed.

=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item                                                                                                          

proxy_test.py ip: 144.202.4.246
hostname: 144-202-4-246.lum-int.io
city: Piscataway
region: New Jersey
country: US
loc: 40.4993,-74.3990
org: AS20473 The Constant Company, LLC
postal: 08854
timezone: America/New_York
readme: https://ipinfo.io/missingauth
.

==================================================== 1 passed in 3.25s ====================================================

Controlling Your Location

With our proxies, you can even choose your location. You can do this by using the country flag. Each country has a two letter country code that you can pass into the proxy.

pytest proxy_test.py --proxy=brd-customer-<YOUR-USERNAME>-zone-<YOUR-ZONE-NAME>:<YOUR-PASSWORD>[email protected]:33335 -s

When you use es (Spain) as your country code, you get routed through a proxy in Spain. You can verify this in the output below.

=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item                                                                                                          

proxy_test.py ip: 176.119.14.158
city: Paracuellos de Jarama
region: Madrid
country: ES
loc: 40.5035,-3.5278
org: AS203020 HostRoyale Technologies Pvt Ltd
postal: 28860
timezone: Europe/Madrid
readme: https://ipinfo.io/missingauth
.

==================================================== 1 passed in 3.98s ====================================================

You can view our geolocation docs here.

Rotating Proxies

With only the basics of Python, you can make a pretty good proxy system. In the code below, we use a set of country codes, but these can easily be replaced with actual proxy IPs. countries holds our list of country codes. We then iterate through them and run our proxy test using all four country codes.

  • us: United States
  • es: Spain
  • il: Israel
  • gb: Great Britain
import subprocess

#list of country codes
countries = [
    "us",
    "es",
    "il",
    "gb",
]

#iterate through the countries and make a shell command for each one
for country in countries:
    command = f"pytest proxy_test.py --proxy=brd-customer-<YOUR-USERNAME>-<YOUR-ZONE-NAME>-country-{country}:[email protected]:33335 -s"

    #run the shell command
    subprocess.run(command, shell=True)

You can run this as a regular Python file.

python rotate_proxies.py

When you run the code, you should receive output similar to this.

(Linux uses --headless by default. To override, use --headed / --gui. For Xvfb mode instead, use --xvfb. Or you can hide this info by using --headless / --headless2 / --uc.)
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item                                                                                                          

proxy_test.py ip: 164.90.142.33
city: Clifton
region: New Jersey
country: US
loc: 40.8344,-74.1377
org: AS14061 DigitalOcean, LLC
postal: 07014
timezone: America/New_York
readme: https://ipinfo.io/missingauth
.

==================================================== 1 passed in 3.84s ====================================================
(Linux uses --headless by default. To override, use --headed / --gui. For Xvfb mode instead, use --xvfb. Or you can hide this info by using --headless / --headless2 / --uc.)
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item                                                                                                          

proxy_test.py ip: 5.180.9.15
city: Madrid
region: Madrid
country: ES
loc: 40.4066,-3.6724
org: AS203020 HostRoyale Technologies Pvt Ltd
postal: 28007
timezone: Europe/Madrid
readme: https://ipinfo.io/missingauth
.

==================================================== 1 passed in 3.60s ====================================================
(Linux uses --headless by default. To override, use --headed / --gui. For Xvfb mode instead, use --xvfb. Or you can hide this info by using --headless / --headless2 / --uc.)
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item                                                                                                          

proxy_test.py ip: 64.79.233.151
city: Tel Aviv
region: Tel Aviv
country: IL
loc: 32.0809,34.7806
org: AS9009 M247 Europe SRL
timezone: Asia/Jerusalem
readme: https://ipinfo.io/missingauth
.

==================================================== 1 passed in 3.36s ====================================================
(Linux uses --headless by default. To override, use --headed / --gui. For Xvfb mode instead, use --xvfb. Or you can hide this info by using --headless / --headless2 / --uc.)
=================================================== test session starts ===================================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/nultinator/clients/bright-data/seleniumbase-proxies
plugins: html-4.0.2, metadata-3.1.1, anyio-4.0.0, seleniumbase-4.34.6, ordering-0.6, rerunfailures-15.0, xdist-3.6.1
collected 1 item                                                                                                          

proxy_test.py ip: 185.37.3.107
city: London
region: England
country: GB
loc: 51.5085,-0.1257
org: AS9009 M247 Europe SRL
postal: E1W
timezone: Europe/London
readme: https://ipinfo.io/missingauth
.

==================================================== 1 passed in 2.90s ====================================================

As you can see, with a very small amount of code, we’re controlling proxies in the US, Spain, Israel, and Great Britain.

Conclusion

When scraping the web, SeleniumBase opens up capabilities in Selenium that many people find impossible. Throughout this guide, you’ve learned how to configure proxies, control your geolocation, and even rotate proxies. These tools will help you get past most of the blocking mechanisms that come your way.

Unlock the full potential of Selenium-based scraping with Bright Data’s industry-leading proxy services. Start your free trial today and experience seamless, reliable data collection at scale!

No credit card required