nodriver proxy with a username and password
Chrome won't read a login from --proxy-server, and nodriver doesn't raise when the page fails. Hand the login to create_context() or answer the proxy's challenge over CDP.
In short
Don’t put USER:PASS@ into --proxy-server. Call browser.create_context(url, proxy_server="http://USER:PASS@host:port"), which also works for SOCKS5, or keep the bare flag and answer Fetch.authRequired once per request. Either way, check what the tab shows: nodriver doesn’t raise when the proxy fails.
nodriver starts a normal Chrome and talks to it over the DevTools protocol. A proxy goes into browser_args as --proxy-server, and that flag takes a scheme, a host and a port. Chrome rejects a value with a login in it, the same way it does for Puppeteer: ERR_NO_SUPPORTED_PROXIES.
The difference is how quietly it happens. await browser.get(url) returns a tab either way. With the login in the flag the tab holds Chrome’s error page; without it, the proxy answers 407, a headless Chrome has nobody to show the login prompt to, and the tab stays empty. A script that reads document.body.innerText gets an error text or an empty string and carries on.
We ran each variant with nodriver and zendriver against our gateway (HTTP on 9000, SOCKS5 on 11000), headless, loading ipinfo.io/json. Both libraries gave the same result in every row.
| Where the proxy goes | What the tab shows |
|---|---|
--proxy-server=http://USER:PASS@…:9000 | error page, ERR_NO_SUPPORTED_PROXIES |
--proxy-server=http://…:9000 | empty page (407 unanswered) |
--proxy-server=http://…:9000 + Fetch handler | 200 · country DE |
create_context(proxy_server="http://USER:PASS@…:9000") | 200 · country DE |
create_context(proxy_server="socks5://USER:PASS@…:11000") | 200 · country DE |
create_context(…) with a wrong password | error page, ERR_TUNNEL_CONNECTION_FAILED |
A login in the flag
Chrome doesn’t read credentials out of --proxy-server and refuses the whole value. Every navigation shows the error page; no connection to the proxy is even tried.
The bare flag
Chrome connects, the proxy asks for a login, and nothing in the script answers it. In our headless runs the page simply never filled: an empty body, no error text, no exception.
The usual answer on GitHub is to enable the Fetch domain with handle_auth_requests=True and reply to every Fetch.authRequired with the login. That works while the login is right. With a wrong password, Chrome asks again and the handler answers again with the same wrong password:
Remember which requests you already answered. A second challenge for the same request means the login was refused: cancel it. Then the tab shows a 407 page you can detect, instead of a script that hangs until something else times it out.
Two setups that work
Both ran unchanged with Python 3.12 against our gateway, with nodriver and, after changing the import, zendriver. PROXY_USER and PROXY_PASS come from the environment.
1. create_context() with the login
create_context() opens a new browser context and, when the proxy has a login, starts a small forwarder on 127.0.0.1 that adds it. Chrome gets the forwarder’s address without credentials. The nodriver docs mark the parameter as experimental; it worked for HTTP and SOCKS5 in our runs.
import os
import nodriver as uc # pip install nodriver
PROXY = f"http://{os.environ['PROXY_USER']}-country-de:{os.environ['PROXY_PASS']}@via.proxies.vision:9000"
async def main():
browser = await uc.start(headless=True)
try:
# create_context() opens a new browser context behind a local forwarder
# that adds the login, so Chrome itself never sees the credentials.
tab = await browser.create_context("https://ipinfo.io/json", proxy_server=PROXY)
await tab.sleep(2)
print(await tab.evaluate("document.body.innerText"))
finally:
browser.stop()
uc.loop().run_until_complete(main())2. The flag plus a Fetch handler that gives up
No forwarder: Chrome talks to the proxy itself and the handler supplies the login. HTTP proxies only, since Chrome can’t send a SOCKS5 login.
import os
import nodriver as uc # pip install nodriver
USER = f"{os.environ['PROXY_USER']}-country-de"
PASS = os.environ["PROXY_PASS"]
async def main():
# Only scheme, host and port go into the flag; Chrome rejects a login there.
browser = await uc.start(headless=True, browser_args=["--proxy-server=http://via.proxies.vision:9000"])
try:
tab = browser.main_tab
answered = set()
async def on_auth(event: uc.cdp.fetch.AuthRequired):
# Answer each request once. A second challenge means the login was
# wrong: cancel instead of sending it again (Chrome asks forever).
response = "CancelAuth" if event.request_id in answered else "ProvideCredentials"
answered.add(event.request_id)
await tab.send(uc.cdp.fetch.continue_with_auth(
event.request_id,
uc.cdp.fetch.AuthChallengeResponse(response, USER, PASS),
))
async def on_paused(event: uc.cdp.fetch.RequestPaused):
try:
await tab.send(uc.cdp.fetch.continue_request(event.request_id))
except uc.ProtocolException:
pass # a late request while the browser shuts down
tab.add_handler(uc.cdp.fetch.AuthRequired, on_auth)
tab.add_handler(uc.cdp.fetch.RequestPaused, on_paused)
await tab.send(uc.cdp.fetch.enable(handle_auth_requests=True))
await tab.get("https://ipinfo.io/json")
await tab.sleep(2)
print(await tab.evaluate("document.body.innerText"))
finally:
browser.stop()
uc.loop().run_until_complete(main()){
"city": "Frankfurt am Main",
"country": "DE",
…uc.loop().run_until_complete() instead of asyncio.run() is nodriver’s own recommendation; with asyncio.run() both scripts printed the page and then a RuntimeError: Event loop is closed from nodriver’s cleanup.
For zendriver, change the import to import zendriver as uc. In the second script, also write except uc.core.connection.ProtocolException: zendriver doesn’t export it at the top level. Targeting stays in the username: for one IP across pages add a session and use the sticky port, USER-country-de-session-job1-lifetime-10 on 10000. The same problem in Selenium.
How do I use a proxy with a username and password in nodriver?
Why does nodriver show a blank page or ERR_NO_SUPPORTED_PROXIES?
Does nodriver support SOCKS5 proxies with authentication?
Does the same code work with zendriver?
Keep reading
aiohttp Proxy Errors: ClientHttpProxyError 407, SOCKS5 and trust_env
aiohttp's ClientHttpProxyError explained, why its message leaks your proxy password, socks5:// without aiohttp-socks, and rotation.
6 min read · #python#errors
Missing dependencies for SOCKS support: the Fix, and Why You See It Without SOCKS
Install requests[socks], or find the environment variable that asked for SOCKS. Plus socks5 vs socks5h and the errors that come next.
6 min read · #python#socks5
PHP cURL Proxy With Authentication: CURLOPT_PROXY, 407 and Guzzle
A PHP cURL proxy with username and password: CURLOPT_PROXYUSERPWD, why a 407 isn't always an error, SOCKS5, and the Guzzle setting that skips the proxy.
6 min read · #authentication#errors
Residential proxies, pay per GB
HTTP and SOCKS5. Country, city and sticky sessions in the username. No subscription.