How to Use Mobile Proxieswith Selenium
Selenium WebDriver does not natively support authenticated proxies. This guide shows you how to configure Chrome and Firefox with HTTP and SOCKS5 mobile proxies using working Python code.
Chrome + HTTP Proxy with selenium-wire
Standard Selenium cannot pass proxy credentials (username and password) to Chrome. The selenium-wire library wraps Selenium and intercepts requests, allowing you to inject authentication headers. Install it with:
pip install selenium-wire
Replace the placeholder values with your proxy credentials from the dashboard. You can retrieve them via the API:
# GET https://buy.mobileproxies.org/api/v1/proxies
# Returns: [{ "hostname": "...", "http_port": ..., "username": "...", "password": "...", ... }]Full working example:
from seleniumwire import webdriver
options = {
'proxy': {
'http': 'http://USERNAME:PASSWORD@HOSTNAME:HTTP_PORT',
'https': 'http://USERNAME:PASSWORD@HOSTNAME:HTTP_PORT',
}
}
driver = webdriver.Chrome(seleniumwire_options=options)
driver.get('https://httpbin.org/ip')
print(driver.page_source)
driver.quit()How it works: selenium-wire starts a local man-in-the-middle proxy that adds your credentials to every outgoing request. Chrome connects to selenium-wire's local proxy, which then forwards traffic to your mobile proxy with authentication.
Firefox + SOCKS5 Proxy
Firefox allows direct proxy configuration through its profile preferences. No additional libraries are needed for SOCKS5. Note that Firefox profile-based SOCKS configuration does not support inline username/password authentication — use IP-whitelisted proxies or a separate auth method for SOCKS5.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', 'HOSTNAME')
profile.set_preference('network.proxy.socks_port', SOCKS5_PORT)
profile.set_preference('network.proxy.socks_version', 5)
profile.set_preference('network.proxy.socks_remote_dns', True)
options = Options()
options.profile = profile
driver = webdriver.Firefox(options=options)
driver.get('https://httpbin.org/ip')
print(driver.page_source)
driver.quit()Remote DNS: Setting network.proxy.socks_remote_dns to True ensures DNS queries go through the proxy, preventing DNS leaks that would reveal your real location.
IP Rotation During a Session
You can trigger an IP rotation mid-test by calling the mobileproxies.org rotation API. The modem disconnects and reconnects to the carrier network, assigning a completely new IP. Use the requests library alongside Selenium to make the API call:
import time
import requests
from seleniumwire import webdriver
API_KEY = 'YOUR_API_KEY'
SLOT_ID = 'YOUR_SLOT_ID'
PROXY_HOST = 'HOSTNAME'
PROXY_PORT = 'HTTP_PORT'
PROXY_USER = 'USERNAME'
PROXY_PASS = 'PASSWORD'
options = {
'proxy': {
'http': f'http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}',
'https': f'http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}',
}
}
driver = webdriver.Chrome(seleniumwire_options=options)
# Step 1: Visit a page with the current IP
driver.get('https://httpbin.org/ip')
print('Before rotation:', driver.page_source)
# Step 2: Trigger IP rotation via the API
resp = requests.post(
f'https://buy.mobileproxies.org/api/v1/proxies/{SLOT_ID}/switch',
headers={'Authorization': f'Bearer {API_KEY}'}
)
print('Rotation response:', resp.status_code)
# Step 3: Wait for the modem to reconnect (typically 10-30 seconds)
time.sleep(15)
# Step 4: Continue browsing with the new IP
driver.get('https://httpbin.org/ip')
print('After rotation:', driver.page_source)
driver.quit()Timing: The modem reconnection takes 10–30 seconds. If you send requests too early, they will fail because the proxy is offline during the switch. Build in a retry loop or a sufficient sleep.
Important Notes
Why selenium-wire for Chrome?
Chrome's --proxy-server flag accepts a proxy address but provides no way to pass credentials. When the proxy requires authentication, Chrome shows a native auth dialog that Selenium cannot interact with. selenium-wire solves this by handling authentication at the network layer.
Alternative: Chrome extension for auth
Another approach is to create a Chrome extension that injects proxy credentials via the chrome.webRequest.onAuthRequired event. This avoids the selenium-wire dependency but requires generating and loading a .crx file at runtime. selenium-wire is simpler for most use cases.
Headless mode
Both examples work in headless mode. For Chrome, add chrome_options.add_argument('--headless=new'). For Firefox, add options.add_argument('--headless'). Proxy configuration works identically in headless mode.
Getting your proxy credentials
Fetch your proxy details programmatically from the API:
import requests
resp = requests.get(
'https://buy.mobileproxies.org/api/v1/proxies',
headers={'Authorization': 'Bearer YOUR_API_KEY'}
)
proxies = resp.json()
# Each proxy object contains:
# hostname, http_port, socks5_port, username, password,
# country, region, carrier, ip, slot_id, type
proxy = proxies[0]
print(f"http://{proxy['username']}:{proxy['password']}@{proxy['hostname']}:{proxy['http_port']}")Ready to automate with mobile proxies?
Get proxy credentials with HTTP and SOCKS5 endpoints, IP rotation API access, and carrier-grade IPs that pass anti-bot detection.