Selenium with authenticated proxies done cleanly
The option that isn’t there
Open the Selenium documentation for proxies and you’ll find a proxy object with fields for the HTTP proxy, the SSL proxy, a SOCKS proxy and a bypass list. You won’t find a field for an HTTP proxy username and password. That gap is where most Selenium proxy trouble starts.
Almost every commercial proxy provider authenticates with a username and password by default. Many of them also encode session and targeting options inside the username. So the most common way to buy proxies and the most common browser automation tool don’t quite fit together, and the internet is full of snippets that try to paper over it. Some of those still work. Some worked three Chrome versions ago. A few leak credentials into places you’d rather they didn’t go.
This piece is about that one limitation and the realistic ways around it. It’s narrower than our general proxy authentication methods piece, which compares allowlisting and credentials in the abstract. Here the question is specific: given Selenium, and given a provider that wants a username and password, which setup should you actually run?
There are three families of answer. You can remove the need for credentials by allowlisting your server’s IP with the provider. You can put a small local forwarder in front of the browser that holds the credentials and exposes a plain port. Or you can use a driver-level helper, such as a browser extension or the DevTools and BiDi hooks in newer Selenium versions, to answer the proxy’s challenge from inside the session.
Each works. Each has a cost, and the costs land in different places: on how you deploy, on how often things break when browsers update, and on where the password ends up sitting. We run proxy infrastructure and the automation that uses it, and the answer we’d give most teams is at the end. Before that, it’s worth seeing why the obvious approach fails.
Why the browser can’t just take a URL with a password
The instinct is to pass the proxy as http://user:pass@host:port, the way you would in curl or Python. With Selenium and Chrome, that goes through the --proxy-server switch or the proxy capability, and Chrome’s proxy configuration takes a scheme, a host and a port. The credentials part is dropped.
What happens next is a challenge. The browser connects to the proxy without credentials, and the proxy replies with a 407 Proxy Authentication Required. A person using the browser would see a login dialog and type the password. Selenium can’t see that dialog. It’s a browser-native prompt, outside the page, so it’s not an element you can find, and it’s not a JavaScript alert that the alert API can handle. The automation sits there, or times out, or loads an error page, depending on the browser and version.
Firefox behaves similarly. You can set its proxy host and port through preferences, and it will still ask for credentials through its own prompt when the proxy challenges.
The WebDriver specification does include SOCKS username and password fields in its proxy capability. Whether a browser honours them is up to the browser, and Chrome doesn’t support SOCKS5 with authentication at all. So SOCKS doesn’t rescue you either, at least not in Chrome.
The takeaway is that the proxy address is ordinary configuration, and the credentials are an answer to a challenge. Every working approach either removes the challenge or finds something that can answer it. That framing makes the three options easier to compare, so hold on to it.
Option one: allowlist the machine instead
Most providers let you register one or more IP addresses on your account. Requests from those addresses are accepted without credentials. The proxy never sends a 407, so there’s nothing for Selenium to answer. You pass a plain host:port and the browser just works.
It’s the cleanest setup when it fits. No password sits in the browser profile, in an extension, or in a driver helper. There’s no extra process to run. Every browser, headless or not, Chrome or Firefox, handles it with the standard proxy setting. And because nothing about it depends on browser internals, a browser update won’t break it.
The limits are about where your automation runs. Allowlisting assumes your server’s public IP holds still. A dedicated server or a cloud instance with a reserved address is fine. A laptop on home broadband, a CI runner that gets a new address each job, or a container platform that egresses through a shared, changing NAT address won’t work reliably. Some providers cap how many addresses you can register, and changes can take a few minutes to apply.
There’s also a subtler cost with gateway-style providers. If the provider normally uses the username to pick the country, the session, or the rotation mode, allowlisting takes that control away, because there’s no username. Some providers handle this by offering different ports for different targeting options on allowlisted accounts. Others don’t, and then allowlisting means one behaviour for everything. Ask before you commit.
Finally, allowlisting is account-wide. Anything running on that server can use your proxy allowance, including jobs you didn’t mean to route through it. On a shared box, that’s worth thinking about.
When allowlisting is the right call
Pulling that together, allowlisting is the right first choice when three things are true at once.
Your automation runs on a machine with a stable public address that you control. Your job doesn’t depend on per-session targeting through the username, or your provider offers port-based targeting for allowlisted accounts. And the machine isn’t shared with other workloads that might use the proxy by accident.
If all of that holds, stop reading about workarounds and use it. It’s the setup with the fewest moving parts, and fewer moving parts is the main thing you want in browser automation, which already has plenty.
A few practical notes if you go this way. Register the address you actually egress from, which on cloud platforms isn’t always the address the instance thinks it has. Check with an IP echo request from the same machine before you add it. Keep a note in the repo that says the job depends on allowlisting and which address is registered, because the day someone moves the job to a new server is the day it silently stops working. It won’t show a 407 in that case, necessarily. Depending on the provider, it may just refuse connections, and Selenium will report a generic page-load error that doesn’t mention authentication at all.
And if you use allowlisting for one environment, you can still use a different approach elsewhere. Production on a fixed server might allowlist, while developers’ laptops use a local forwarder. The Selenium code can stay the same in both, pointed at a host and port from configuration.
Option two: a local forwarder that holds the credentials
The second approach moves the credentials out of the browser entirely. You run a small proxy on the same machine, listening on a local port without authentication. The browser points at that local port. The forwarder takes each connection and passes it upstream to the provider, adding the credentials on the way.
From Selenium’s point of view, this is identical to allowlisting: a plain host:port with no challenge. From the provider’s point of view, every connection arrives properly authenticated.
There are plenty of ways to run one. General-purpose proxy servers that support an upstream or parent proxy with credentials will do it, as will debugging proxies that have an upstream mode. It can also be a few dozen lines of your own code for plain CONNECT tunnelling. The choice matters less than the configuration: bind only to the loopback address, never to all interfaces, or you’ve just opened an unauthenticated relay into your proxy account for anyone who can reach the port.
A forwarder also solves problems beyond Selenium. Every tool on the machine can use the same local port without knowing the provider’s credentials. Swapping providers becomes a change to the forwarder’s config rather than to every script. And you can run several local ports, each mapped to a different upstream username, so one port means one exit or one country. The browser’s side stays simple.
This pattern is covered in more depth in its own piece on this site, which looks at the forwarder as general infrastructure. The Selenium-specific part is simply that it removes the missing-credentials problem for every browser, in every mode, without touching browser internals.
The forwarder’s costs and the Grid trap
A forwarder is one more process to run, start before the browser, monitor, and restart when it dies. If it goes down, every browser pointed at it fails, and the failure looks like a proxy connection error rather than anything that names the forwarder. Health-check it at startup the same way you’d check the upstream.
It also sits in the path of every byte. A well-behaved forwarder that just tunnels CONNECT requests adds little, but a debugging proxy that decrypts traffic to inspect it is a different matter. That kind needs its own certificate trusted by the browser, adds work to every request, and changes the TLS the target sees, because the target is now talking to the debugging proxy’s TLS stack rather than the browser’s. For a scraping job, plain tunnelling is what you want. Only decrypt when you’re actually debugging.
Then there’s the trap that catches almost everyone who moves to Selenium Grid or a remote browser service. With a local forwarder, the browser is configured to use 127.0.0.1:port. On a single machine, that’s the forwarder. On a Grid, the browser runs on a node, and 127.0.0.1 from the browser’s point of view is the node, not the machine running your test script. If the forwarder runs next to your script, the browser can’t reach it.
So on a Grid, the forwarder has to run on each node, or somewhere the nodes can reach over a private network, with the browser pointed at that address. The second option means the forwarder is listening on a non-loopback interface, which brings back the open-relay risk, so it needs firewall rules that only admit the nodes. None of this is hard. It just has to be designed, and the failure when it isn’t is a browser that can’t connect, with an error that mentions nothing about Grid.
Option three: a helper that answers the challenge
The third family keeps the credentials with the browser session and answers the 407 when it arrives. There are two main versions of this in current use.
The first is a small browser extension. Chrome extensions can listen for authentication requests and supply credentials, and people build a tiny extension on the fly, with the proxy credentials written into its files, and load it into the browser at startup. It works with plain Selenium and no extra process. The costs are real, though. Chrome’s move from Manifest V2 to V3 broke many of the older snippets people still copy, and the permissions needed for answering auth requests changed along the way. Headless mode’s support for extensions has varied across Chrome versions. And the credentials sit in plain files in a temporary extension folder, which you then need to clean up.
The second is the DevTools or BiDi route in Selenium 4 and later. Selenium exposes ways to register an authentication handler that answers challenges through the browser’s debugging protocol, rather than through a dialog. How it’s spelled differs by language binding and by Selenium version, and some bindings have had it longer than others. Where it’s available and it handles proxy challenges as well as site challenges in your version, it’s a tidy option: credentials in code, no extension, no extra process.
There used to be a third popular choice, a Python package that wrapped Selenium with its own intercepting proxy. It’s no longer maintained, and building a new job on an unmaintained package in the network path isn’t something we’d recommend. If you have one running, plan a move to one of the options above.
Why helpers break more often than the other two
Helpers are attractive because they keep everything inside one script. The trade is that they depend on browser and driver internals, and those change often.
An extension-based helper depends on the extension platform: which manifest versions are accepted, which permissions it needs, whether headless loads it, and whether an enterprise policy on the machine blocks unpacked extensions. Chrome ships a new major version roughly every month, and while most releases don’t touch any of that, the ones that do tend to break things without an obvious error. The extension silently fails to load, the challenge goes unanswered, and the page never loads.
A DevTools or BiDi handler depends on the protocol support in your Selenium version and your browser version together. It’s usually Chromium-first, so behaviour in Firefox may differ or lag. When the browser updates ahead of Selenium, or the other way round, the handler can stop being called. Again, nothing announces it.
Neither of these is a reason to avoid helpers. They’re a reason to pin versions of the browser, the driver and Selenium together, and to run a startup check that proves the proxy route works before the real job begins. With allowlisting or a forwarder, a browser update almost never breaks proxy auth, because the browser isn’t involved in it. With a helper, it’s the browser’s own machinery doing the work, so its updates are your problem.
If your deployment pins everything and upgrades deliberately, helpers are fine. If browsers update themselves on the machines that run your jobs, lean towards the forwarder.
Where the password ends up living
It’s worth comparing the three options by a question people often skip: after the job starts, where is the proxy password sitting?
With allowlisting, nowhere. There’s no password in use. That’s the strongest reason to prefer it when it fits.
With a forwarder, it lives in the forwarder’s configuration or environment on that machine, one place, readable by whoever can read that config. Scripts and browsers never see it. If the credentials rotate, you change one file and restart one process.
With an extension, it’s written into files inside a generated extension folder, usually in a temporary directory, and loaded into the browser profile. If those folders aren’t cleaned up, every past run leaves a copy of the password on disk. If the machine is shared, other users may be able to read them.
With a DevTools or BiDi handler, it’s in your script’s memory and wherever your script got it from, typically an environment variable or a secrets store. That’s reasonable, as long as the script doesn’t log it. Selenium’s own logging and your framework’s debug output can include capability objects and command payloads, so check what gets written at your log level before assuming the password stays in memory.
For all four, also check your error handling. Stack traces that print the full proxy URL or the capability dictionary are one of the most common ways credentials end up in shared logs. A companion piece on this site covers keeping proxy credentials out of git and logs more broadly, so we won’t repeat it here.
Rotation and changing exits in Selenium
Whichever option you pick, Selenium’s proxy is set when the driver session starts. You can’t change it on a running session. That shapes how rotation works.
To use a different exit, you end the session and start a new one with a different configuration. With allowlisting on a gateway, that means a different port if the provider maps ports to targeting. With a forwarder, it means pointing the new session at a different local port, each mapped to a different upstream username. With a helper, it means passing different credentials to the new session.
Starting a browser session is slow compared with an HTTP request, so a Selenium job that rotates on every page will spend a lot of its time launching browsers. That’s usually a sign the job is using a browser for pages that don’t need one. Our piece on rotating without losing your session makes the broader case that most browser jobs should hold one exit for a whole task rather than rotate within it.
There’s one pattern that looks like mid-session rotation and isn’t. A rotating gateway that hands out a new exit per connection will change your address underneath a running Selenium session without the browser knowing. The session’s cookies stay, the address changes, and the target sees a logged-in session hopping networks. If you’re using a gateway like that for browser work, ask the provider for sticky sessions and set the stickiness longer than your longest task.
And if your job does need many exits in parallel, remember each session is a full browser. Memory, not the proxy, is usually the first limit you’ll hit.
Firefox is its own case
Most Selenium proxy advice assumes Chrome, and a surprising amount of it doesn’t transfer to Firefox. If your jobs run on Firefox, or on both, it’s worth knowing where they differ.
Firefox takes its proxy through preferences rather than a command-line switch. Through Selenium you’d set them on the Firefox options object, or through the standard proxy capability, which geckodriver translates into those preferences. Host and port for HTTP, for SSL, and for SOCKS each have their own preference, and there’s a separate preference that decides whether DNS lookups go through the SOCKS proxy or happen locally. Leave that one at its default without checking, and a SOCKS setup can resolve hostnames on the machine rather than at the exit.
Authentication still produces a native prompt. Selenium can’t type into it any more than it can in Chrome. So the same three options apply, with different details. Allowlisting and a local forwarder work identically, because Firefox never sees a challenge. Extension-based helpers need a Firefox extension, which is packaged and loaded differently from a Chrome one, and a snippet written for Chrome won’t simply run. DevTools-based helpers are generally Chromium-first, and BiDi support in Firefox has been arriving at its own pace, so check what your Selenium binding and Firefox version actually support before relying on it.
That asymmetry is another argument for the first two options. If a job has to run on more than one browser, allowlisting or a forwarder keeps the proxy setup identical across all of them. Helpers mean maintaining a different mechanism per browser, each with its own way of breaking on update.
One more Firefox detail: profiles. If you reuse a Firefox profile between runs to keep cookies, any proxy preferences saved in it can override or conflict with what you set through Selenium. Start each job from a clean profile, or set every proxy preference explicitly, so the profile can’t carry an old exit into a new run.
Testing what happens when credentials fail
Most setups get tested for the success path. It’s worth spending ten minutes on the failure path too, because credentials do fail: a password gets rotated, a trial ends, a sub-user gets disabled, a plan runs out of bandwidth.
With a helper, a wrong password usually means the proxy challenges again after the helper answers. Depending on the helper, that becomes a loop of retried challenges, a hang, or an error page. With a forwarder, the forwarder receives the 407 from upstream and passes some error back to the browser, which may show up as a tunnel failure rather than an authentication error. With allowlisting, a removed address typically shows up as refused connections.
None of those messages says “your password expired” in plain words. So trigger each failure on purpose once: use a deliberately wrong password with a helper, a wrong upstream password in the forwarder, and a machine that isn’t allowlisted. Note what your job’s logs show in each case, and write that down next to the setup. When it happens for real at 3am, whoever is on call will recognise it.
Then make the job fail fast. A browser session that waits for a page timeout on every URL because credentials are wrong can burn an hour before anyone notices. The startup echo check is the natural place: if it fails, stop the job and raise an alert, rather than letting every page discover the problem separately.
Confirming the route before the job runs
Whatever setup you choose, prove it from inside the browser before the real work begins. A curl test from the same machine proves the provider works. It doesn’t prove the browser is using the route you configured, which is what matters. Our piece on why a proxy works in curl but fails in browser automation is all about that gap.
The simplest check is to point the fresh session at an IP echo page you trust, read the page text, and compare it with the exit you expected and with the server’s own address. If it shows your own address, the browser went direct. If it shows an error, read which kind: a connection error to the proxy, a tunnel failure, or a timeout on the page itself.
With a forwarder, check both hops. First, that the forwarder is up and reaching the provider, by sending a single request through it outside the browser. Then, that the browser reaches the forwarder, with the echo page. When something fails, you’ll know which half.
With a helper, the check also proves the helper loaded and answered the challenge. That’s the one to keep in the startup path permanently, because it’s the one that browser updates break.
Log the exit address, without credentials, at the top of each run. When a batch of results looks wrong a week later, knowing which exit produced it saves a lot of guessing.
This check confirms routing. It says nothing about how a target will treat that exit, and no Selenium setting changes that. A site that rate limits your sessions is reacting to how many and how fast, and the durable response is pacing that fits what the site allows.
Which one we’d pick
For most teams, the order of preference is straightforward.
If your automation runs on a machine with a stable address and you don’t need per-session targeting through usernames, allowlist it. Fewest parts, nothing to leak, nothing that browser updates can break.
If you can’t allowlist, because the address moves, you run on laptops or CI, or you need username-based targeting, run a local forwarder bound to loopback. It works for every browser, in every mode, and it keeps the password in one place. On a Grid, run it on the nodes.
Use a driver-level helper when you can’t run an extra process and can’t allowlist, and when you’re willing to pin browser, driver and Selenium versions together and keep a startup check that proves the helper works. It’s a legitimate choice. It’s also the one that will need the most attention over time.
Mixing is fine, and common. A team might allowlist its production servers, give developers a forwarder on their laptops, and keep a helper only for one legacy job that can’t change yet. What matters is that each job’s choice is written down next to its code, with the reason, so nobody has to rediscover it from a failing run. The Selenium side should read the proxy host and port from configuration in every case, so moving a job from one option to another is a config change rather than a code change.
We’d push back on one thing that shows up in a lot of tutorials: solving the credentials problem by switching to a proxy product purely because it supports allowlisting, without checking whether that product suits the job. The authentication method is the easy part to change. The proxy type, the provider’s sourcing and the fit with your targets matter more, and they deserve their own decision.
Setting it up on Monday
If you have Selenium jobs running against an authenticated proxy right now, here’s a short review.
Find out how each job currently answers the proxy’s challenge. If the answer is “an old snippet from a forum”, check whether it still works on your current browser version, and whether it depends on an unmaintained package.
Write down where each job runs and whether that machine has a stable public address. For the ones that do, ask your provider about allowlisting and about port-based targeting for allowlisted accounts.
For the rest, set up a forwarder on loopback, one local port per exit you need, and point the browser at it. If you’re on a Grid, put the forwarder on the nodes and firewall it.
If you keep a helper, pin the browser, driver and Selenium versions in your deployment, and upgrade them together on purpose.
Add the echo-page check to the start of every session and log the exit with each run. Clean up any temporary extension folders that hold credentials.
None of this makes a scraper harder to detect or guarantees a target will accept it. It makes the proxy you’re paying for the one the browser actually uses, with the password in the fewest places possible.
For more on proxy setup for browser automation, from Playwright to provider testing, head to Proxy Scraping.
Get new guides and videos first — join the Telegram channel.