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.
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
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.
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.
You’ll receive a response similar to this.
Proxy Configuration
To use a proxy, we simply need to use the --proxy
flag followed by our proxy url. Look at the format below.
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
.
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.
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:
- geo-targeting
- Rotating proxies
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.
When we run it we get the following output. If you look closely, you’ll see that our location has changed.
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.
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.
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 Stateses
: Spainil
: Israelgb
: Great Britain
You can run this as a regular Python file.
When you run the code, you should receive output similar to this.
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