Quick answer
How do I use a proxy with a username and password in Selenium?
CrawlRelay works in Selenium through a 46-line local forwarder: it listens on 127.0.0.1:8899, adds the Proxy-Authorization header and relays to gw.crawlrelay.com:7777. Chrome then runs with --proxy-server=http://127.0.0.1:8899, because its flag accepts no password. A 1 GB test plan costs $1.50.
In brief
- Problem: Chrome's --proxy-server takes a host and port only, and Selenium has no portable proxy-password option.
- Fix: a standard-library Python forwarder on 127.0.0.1:8899 that inserts one header and copies bytes.
- Privacy of the page: HTTPS runs end to end through CONNECT; the forwarder never sees page content in clear text.
- Sessions: the username given to the forwarder decides rotation, or one IP for up to 30 minutes.
- Old options: selenium-wire was archived on 3 January 2024; Playwright takes credentials directly.
What does the Selenium setup look like at a glance?
| Why a forwarder | Chrome's --proxy-server flag accepts no username or password |
|---|---|
| Forwarder | Listens on 127.0.0.1:8899, adds Proxy-Authorization, relays to gw.crawlrelay.com:7777 |
| Chrome option | --proxy-server=http://127.0.0.1:8899 |
| Rotation | Each new CONNECT tunnel is a new connection, so usually a new exit; add a session ID for one IP |
| Simpler alternative | Playwright takes the username and password directly |
How does the local forwarder work?
The forwarder accepts Chrome's proxy request, inserts one header with the credentials, then copies bytes both ways. It accepts the browser's proxy request, inserts one header with your credentials, and then copies bytes in both directions. For an HTTPS site the browser sends CONNECT host:443, the gateway answers 200 Connection established, and TLS then runs end to end between Chrome and the website. The forwarder never sees page content in clear text.
This minimal version handles HTTPS sites, where each connection carries one CONNECT tunnel. That covers almost every website today. Keep it bound to 127.0.0.1 so no other machine can use your credentials.
import asyncio
import base64
import os
UPSTREAM = ("gw.crawlrelay.com", 7777)
USER = os.environ.get("CRAWLRELAY_USER", "USERNAME-country-us")
PASSWORD = os.environ.get("CRAWLRELAY_PASSWORD", "PASSWORD")
AUTH = b"Proxy-Authorization: Basic " + base64.b64encode(("%s:%s" % (USER, PASSWORD)).encode())
async def pipe(reader, writer):
try:
while True:
data = await reader.read(65536)
if not data:
break
writer.write(data)
await writer.drain()
except ConnectionError:
pass
finally:
writer.close()
async def handle(client_reader, client_writer):
try:
head = await client_reader.readuntil(b"\r\n\r\n") # request line and headers from the browser
except asyncio.IncompleteReadError:
client_writer.close()
return
lines = head.split(b"\r\n")
lines.insert(1, AUTH) # add the credentials right after the request line
up_reader, up_writer = await asyncio.open_connection(*UPSTREAM)
up_writer.write(b"\r\n".join(lines))
await up_writer.drain()
await asyncio.gather(pipe(client_reader, up_writer), pipe(up_reader, client_writer))
async def main():
server = await asyncio.start_server(handle, "127.0.0.1", 8899)
async with server:
await server.serve_forever()
if __name__ == "__main__":
asyncio.run(main())How do I point Selenium at the forwarder?
Start forwarder.py, then give Chrome --proxy-server=http://127.0.0.1:8899; Selenium Manager fetches the driver. Selenium Manager is built into Selenium 4.6 and later and downloads a matching ChromeDriver, so there is nothing else to install. Run the forwarder in one terminal and the script below in another.
from selenium import webdriver
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=http://127.0.0.1:8899") # the local forwarder
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
try:
driver.set_page_load_timeout(60)
driver.get("https://api.ipify.org")
print("exit IP:", driver.find_element(By.TAG_NAME, "body").text)
finally:
driver.quit()How do I get one IP per Selenium session, or a new IP per page?
The username given to the forwarder decides: no session part rotates, a session part keeps one IP for up to 30 minutes. The forwarder sends whatever username you give it, so the session behaviour comes from that username:
- Rotating per connection: leave out the session part (
USERNAME-country-us). Chrome opens several connections per page, so a single page can load through several exits. - One IP per Selenium session: set
CRAWLRELAY_USERtoUSERNAME-country-us-session-run42-ttl-30before you start the forwarder. Every connection then leaves through the same IP for up to 30 minutes; the IP can change earlier. - Several sessions in parallel: run one forwarder per session on different local ports, such as 8899 and 8900, and give each WebDriver its own port.
What about selenium-wire or browser extensions?
selenium-wire is no longer an option: its author archived the repository on 3 January 2024 and it receives no updates. It added proxy authentication by intercepting traffic. A browser extension that answers the proxy's authentication request also works, but it must be packaged and loaded into each profile. The forwarder above has no dependencies and works with any browser that Selenium drives. For new projects, Playwright is simpler still: it takes the proxy username and password directly.
Which errors does a missing proxy password cause in Selenium?
A visible login pop-up in headed Chrome, and ERR_TUNNEL_CONNECTION_FAILED in headless mode. Both mean the gateway asked for credentials and Chrome had none to send. CrawlRelay counted 15 proxy errors in its error reference; for Selenium, two matter most: the 407 response the gateway sends, and the tunnel error Chrome shows in its place.
If the forwarder is running and the error persists, test the same username and password with curl against gw.crawlrelay.com:7777. A 407 from curl points to the credentials or a typo in the targeting part; a working curl points to the browser side, usually a wrong local port.
How do I keep the proxy password safe with Selenium?
Keep the forwarder bound to 127.0.0.1, read the password from environment variables, and give each scraper its own sub-user with a traffic limit. Bound to the loopback address, no other machine can use the forwarder, so the credentials never leave the computer except inside the header sent to the gateway.
In the CrawlRelay dashboard, a sub-user has its own username, its own password of 8 to 64 characters and an optional limit in GB. A leaked sub-user password can then spend no more than its limit, and disabling the sub-user stops its login at once without touching the other scrapers.
Selenium and proxies: questions
Why does Chrome show a login pop-up with CrawlRelay in Selenium?
CrawlRelay asked for credentials and Chrome had none, since --proxy-server takes only a host and port. In headless mode the same problem shows as ERR_TUNNEL_CONNECTION_FAILED; the local forwarder fixes both.
Does the forwarder slow down scraping through CrawlRelay?
Hardly. It copies bytes between local sockets and one connection to CrawlRelay, without decrypting anything. Page time depends on the residential exit and the target site.
Can I use Firefox with Selenium and CrawlRelay?
Yes. Set network.proxy.type to 1 and network.proxy.http and network.proxy.ssl to 127.0.0.1 on port 8899 in FirefoxOptions. Keep the forwarder in front of CrawlRelay, because Firefox asks for proxy credentials in a dialog Selenium cannot fill reliably.
Can I run several CrawlRelay sessions in parallel with Selenium?
Yes: start one forwarder per session on its own local port, such as 8899 and 8900, each with its own CrawlRelay username, and give each WebDriver its port.
Verdict
- Keep Selenium plus the forwarder for an existing Selenium codebase: 46 lines, no dependency, any browser.
- Start new browser scrapers in Playwright, which accepts the CrawlRelay username and password with no forwarder.
Sources (2)
- selenium-wire repository, archived by the owner on 3 January 2024 — GitHub (read 2026-11-13)
- Selenium Manager documentation — Selenium project (read 2026-11-13)
Give Selenium a residential exit
$1.50 for 1 GB to test the forwarder, $5.94 for 10 GB.