How to Use IPRoyal Rotating Residential Proxies with Selenium in Python for Web Scraping
A practical guide to integrating IPRoyal's residential proxies with Selenium WebDriver in Python, including authentication, rotation, and troubleshooting.
Overview
Selenium is a popular tool for browser automation, but scraping at scale often requires rotating IP addresses to avoid blocks. IPRoyal's residential proxy network provides a large pool of IPs with flexible rotation options. This tutorial shows you how to connect Selenium to IPRoyal residential proxies using Python, handle authentication, and rotate IPs between requests.
Because Selenium does not natively support proxy authentication in all drivers, we will use the selenium-wire package, which extends Selenium and handles proxy credentials cleanly. The examples below use Chrome, but the same approach works with other browsers supported by Selenium.
Prerequisites
- Python 3.8 or newer
- An IPRoyal account with residential proxy credentials
- Basic familiarity with Python and Selenium
- Chrome browser and ChromeDriver installed (or an alternative browser driver)
Step 1: Collect Your IPRoyal Proxy Credentials
Log in to your IPRoyal dashboard and locate the residential proxy endpoint details. You will need:
- Proxy host (e.g.,
geo.iproyal.comor the host shown in your dashboard) - Port number
- Username
- Password
IPRoyal residential proxies typically offer both rotating and sticky sessions. For rotating sessions, each request (or each new browser session) can use a different IP. For sticky sessions, you can keep the same IP for a period by using a session identifier in the username. Check your dashboard for the exact format—often you append a session ID to the username, such as yourusername-session-abc123.
Step 2: Install Selenium and selenium-wire
Install the required packages using pip:
pip install selenium selenium-wire
Make sure the ChromeDriver version matches your installed Chrome version. You can download ChromeDriver from the Chrome for Testing page or use a manager like webdriver-manager.
Step 3: Configure Selenium with IPRoyal Proxy Authentication
Create a Python script that sets up a Selenium Wire driver with your IPRoyal proxy credentials. Replace the placeholders with your actual values.
from seleniumwire import webdriver
# Replace with your IPRoyal proxy details
proxy_user = "your_username"
proxy_pass = "your_password"
proxy_host = "your-iproyal-host"
proxy_port = "your_port"
# Build the proxy URL
proxy_url = f"http://{proxy_user}:{proxy_pass}@{proxy_host}:{proxy_port}"
# Configure Selenium Wire proxy options
proxy_options = {
'proxy': {
'http': proxy_url,
'https': proxy_url,
'no_proxy': 'localhost,127.0.0.1'
}
}
# Launch Chrome with Selenium Wire
driver = webdriver.Chrome(seleniumwire_options=proxy_options)
try:
driver.get("https://httpbin.org/ip")
print(driver.find_element("tag name", "body").text)
finally:
driver.quit()
Run the script. If the proxy is working, you should see an IP address that belongs to IPRoyal's network, not your real IP.
Step 4: Rotate IPs Between Requests
There are two common ways to rotate IPs with IPRoyal residential proxies:
- Rotating endpoint: Use the same host and port for every request. The proxy automatically assigns a new IP for each connection. This is the simplest method for stateless scraping.
- Sticky sessions with a session ID: Append a unique session ID to your username (e.g.,
yourusername-session-abc123). All requests using that session ID will use the same IP until the session expires or you change the ID. This is useful for multi-step flows like logging in and then scraping data.
To rotate IPs manually in Selenium, you can create a new driver instance with a different session ID for each scraping task. Here is an example that cycles through a list of session IDs:
import time
from seleniumwire import webdriver
base_username = "your_username"
base_password = "your_password"
proxy_host = "your-iproyal-host"
proxy_port = "your_port"
session_ids = ["session1", "session2", "session3"]
for session_id in session_ids:
# Construct username with session ID (adjust syntax per IPRoyal docs)
username = f"{base_username}-session-{session_id}"
proxy_url = f"http://{username}:{base_password}@{proxy_host}:{proxy_port}"
proxy_options = {
'proxy': {
'http': proxy_url,
'https': proxy_url,
'no_proxy': 'localhost,127.0.0.1'
}
}
driver = webdriver.Chrome(seleniumwire_options=proxy_options)
try:
driver.get("https://httpbin.org/ip")
print(f"Session {session_id}:", driver.find_element("tag name", "body").text)
finally:
driver.quit()
time.sleep(1)
If IPRoyal provides a rotating endpoint that changes IP per request, you can skip the session ID logic and reuse the same credentials. Verify the behavior in your dashboard.
Step 5: Handle Geo-Targeting and Sticky Sessions
IPRoyal residential proxies support country and city targeting. You can usually specify the location by adding a country code to the username or by using a dedicated endpoint. For example, many providers use a username format like yourusername-country-us. Check your IPRoyal dashboard for the exact syntax.
To maintain a sticky session for a multi-step scrape (e.g., add to cart, then checkout), use a single session ID across all actions and keep the same driver instance open. Changing the driver or session ID will result in a different IP.
Step 6: Verify the Proxy and Handle Errors
Always verify that your requests are going through the proxy. A simple check is to visit https://httpbin.org/ip or https://ipinfo.io/ip and confirm the IP is not your own. If you need to inspect headers, Selenium Wire allows you to access request and response objects:
for request in driver.requests:
if request.response:
print(request.url, request.response.status_code)
Troubleshooting
| Issue | Likely Cause | Fix |
|---|---|---|
| 407 Proxy Authentication Required | Incorrect username/password or special characters not URL-encoded | URL-encode special characters in username/password (e.g., @ becomes %40). Verify credentials in your IPRoyal dashboard. |
| Connection timeout | Wrong host/port or firewall blocking | Double-check host and port. Try a different port if IPRoyal provides alternatives. Ensure your firewall allows outbound connections. |
| IP not changing | Using sticky session ID or cached connection | Use the rotating endpoint or change the session ID. Close the driver and open a new one. |
| WebDriver error on launch | ChromeDriver version mismatch | Update ChromeDriver to match your Chrome version. |
| SSL errors | Proxy not handling HTTPS correctly | Ensure you are using http:// in the proxy URL even for HTTPS sites (the proxy tunnels HTTPS). |
Summary
In this tutorial, you learned how to integrate IPRoyal residential proxies with Selenium in Python using selenium-wire for authentication. You configured a single proxy, implemented rotation via sticky session IDs, and verified the setup. Remember to use rotating endpoints for high-volume scraping and sticky sessions for multi-step workflows. Always test your proxy connection before running large jobs, and refer to your IPRoyal dashboard for the exact endpoint format and session parameters.