Introduction
Selenium — An Overview
Selenium is a powerful tool for automating web browsers, widely used for web application testing and web scraping. It supports various browsers (Chrome, Firefox, Safari, etc.) and allows you to write test scripts in Python, among other languages. The core component, Selenium WebDriver, interacts with web elements to simulate user actions like clicking, typing, and navigating. Selenium also includes features like Selenium Grid for parallel test execution across different environments, making it a versatile and essential tool for developers and testers. For more information, visit the Selenium documentation.
Prerequisites
Install Python from the official website.
Check if Python is installed correctly, you can use the following command in your terminal or command prompt
python --versionThe output should be in the following format:
Python 3.11.2If you receive an error or see a version number starting with 2.x, you need to download Python 3.x and follow the installation instructions to set it up
Next, create a new Python project and install the Selenium-Python package
pip install seleniumSelenium also needs a web driver. The web driver is a crucial component that allows Selenium to interact with a web browser. It acts as a bridge between Selenium and the browser, enabling automated control and interaction with web pages.
You don't have to install one yourself. Since version 4.6, Selenium ships with Selenium Manager, which detects your browser version and downloads the matching driver the first time you start a browser, so no separate driver package is required.
Let's Start Scraping !
Now, we're ready to scrape https://httpbin.org/ip to grab the IP address from it using Selenium and ProxyPanel for routing traffic through proxies to maintain anonymity and bypass restrictions.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
# define custom options for the Selenium driver
options = Options()
driver = webdriver.Chrome(options=options)
driver.get('https://httpbin.org/ip')
print(driver.find_element(By.TAG_NAME, "body").text)
driver.quit()
The output should be in the following format, showing your own public IP:
{ "origin": "YOUR_PUBLIC_IP" }However, we haven't used a ProxyPanel proxy in this script. Next, we'll configure the proxy to route our traffic through ProxyPanel for enhanced anonymity
Selenium Wire
First, we need to install selenium-wire, which allows us to configure our script to use proxies with authentication. Selenium Wire extends Selenium's functionality, making it easier to handle complex scenarios such as working with authenticated proxies. This is particularly useful when you need to ensure that your web scraping activities are routed through a secure and reliable proxy service like ProxyPanel.
Selenium Wire's author has stopped maintaining it, and its GitHub repository is archived. It still works, but newer releases of its dependency blinker removed a module it imports, so install it together with an older blinker:
pip install selenium-wire "blinker<1.8"Retrieve Proxy Information from Your Dashboard
Open My Proxies in your dashboard. A proxy marked Pending isn't running yet, so press Start proxy on it first.
From the Connection column, note the host and the HTTP port shown in the "HTTP proxy" entry (the "SOCKS5 proxy" entry has a different port). From the Credentials column, use Copy username, then reveal the masked password with the eye button (Show / hide) or copy it with Copy password. No account password is asked for.
Save the details as environment variables for the script to read:
# macOS / Linux
export PROXY_HOST="HOST"
export PROXY_HTTP_PORT="HTTP_PORT"
export PROXY_USERNAME="USERNAME"
export PROXY_PASSWORD="PASSWORD"
# Windows (PowerShell)
$env:PROXY_HOST = "HOST"
$env:PROXY_HTTP_PORT = "HTTP_PORT"
$env:PROXY_USERNAME = "USERNAME"
$env:PROXY_PASSWORD = "PASSWORD"Set Up Proxy Configuration in Your Script
Update your Selenium setup with the proxy information you retrieved from your dashboard. The script below builds the proxy address from the host, HTTP port, username and password in your environment variables, so all browser traffic is routed through the proxy server.
Refer to the following script as a guide:
import os
from seleniumwire import webdriver
from selenium.webdriver.common.by import By
# Proxy configuration for Selenium Wire (the HTTP port)
proxy_username = os.environ['PROXY_USERNAME']
proxy_password = os.environ['PROXY_PASSWORD']
proxy_address = f"{os.environ['PROXY_HOST']}:{os.environ['PROXY_HTTP_PORT']}"
seleniumwire_options = {
'proxy': {
# http:// in both entries: this is a plain HTTP proxy
'http': f'http://{proxy_username}:{proxy_password}@{proxy_address}',
'https': f'http://{proxy_username}:{proxy_password}@{proxy_address}',
},
}
# Initialize Chrome with Selenium Wire and proxy options
driver = webdriver.Chrome(seleniumwire_options=seleniumwire_options)
# Open a test page to verify the proxy setup
driver.get('https://httpbin.org/ip')
print(driver.find_element(By.TAG_NAME, 'body').text)
# Close the browser
driver.quit()Keep http:// in front of both entries, even though the sites you visit use HTTPS. The scheme describes the proxy you connect to, and the HTTP port is a plain HTTP proxy; Selenium Wire takes the scheme of the https entry for its own connection to the proxy. It also copies the username and password out of the URL exactly as written, without decoding them, so enter them as they are rather than percent-encoding them.
You have successfully configured Selenium with the ProxyPanel proxy. The output now shows the proxy's IP address instead of your own:
{ "origin": "PROXY_IP" }