On this page
Selenium handles proxies without a password easily. Proxies with a username and password, like every FreshProxies plan, are awkward: Chrome ignores credentials written into its proxy setting, and WebDriver can't fill in the sign-in prompt that Chrome shows instead. This page explains the options that work and their trade-offs.
Why the obvious setup isn't enough
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=http://HOST:PORT")
driver = webdriver.Chrome(options=options)
driver.get("https://api.ipify.org")
This points Chrome at the proxy, but the proxy needs a username and password. Writing them into --proxy-server doesn't help, because Chrome doesn't use credentials from its proxy setting. Chrome asks for them in a sign-in prompt instead, which Selenium can't answer, so the page doesn't load.
Option 1: a local forwarding proxy
Run a small proxy on your own machine that needs no password and forwards everything to FreshProxies with your credentials. The browser connects to the local address, and the forwarder adds the authentication.
This is the most dependable option. It works with Chrome, Firefox and Edge, in headless and normal mode, and doesn't depend on browser extension rules.
Open-source forwarders such as GOST, used in the example below, do this in one command:
gost -L http://127.0.0.1:8080 -F "http://USERNAME:PASSWORD@HOST:PORT"
Then point the browser at the local port:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=http://127.0.0.1:8080")
driver = webdriver.Chrome(options=options)
For Firefox, set the proxy through preferences:
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.set_preference("network.proxy.type", 1)
options.set_preference("network.proxy.http", "127.0.0.1")
options.set_preference("network.proxy.http_port", 8080)
options.set_preference("network.proxy.ssl", "127.0.0.1")
options.set_preference("network.proxy.ssl_port", 8080)
driver = webdriver.Firefox(options=options)
Two rules:
- Listen on
127.0.0.1only. A forwarder without a password that listens on all interfaces lets anyone on your network use your proxy and your traffic. - For several sticky sessions at once, run one forwarder per sticky line, each on its own local port.
Option 2: a small Chrome extension
A Chrome extension can set the proxy and answer the sign-in prompt itself. Create a folder, for example proxy_auth_extension, with two files.
manifest.json:
{
"manifest_version": 3,
"name": "Proxy auth",
"version": "1.0",
"permissions": ["proxy", "webRequest", "webRequestAuthProvider"],
"host_permissions": ["<all_urls>"],
"background": { "service_worker": "background.js" }
}
background.js:
chrome.proxy.settings.set({
value: {
mode: 'fixed_servers',
rules: {
singleProxy: { scheme: 'http', host: 'HOST', port: PORT }, // PORT as a number
bypassList: ['localhost', '127.0.0.1'],
},
},
scope: 'regular',
});
chrome.webRequest.onAuthRequired.addListener(
(details, callback) => {
if (details.isProxy) {
callback({ authCredentials: { username: 'USERNAME', password: 'PASSWORD' } });
} else {
callback({});
}
},
{ urls: ['<all_urls>'] },
['asyncBlocking']
);
Load it when you start the browser:
from pathlib import Path
from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = "/path/to/chrome-for-testing/chrome"
options.add_argument(f"--load-extension={Path('proxy_auth_extension').resolve()}")
driver = webdriver.Chrome(options=options)
driver.get("https://api.ipify.org")
print(driver.find_element("tag name", "body").text)
The catch: since version 137, regular Google Chrome ignores --load-extension. The flag still works in Chrome for Testing and Chromium, which is why the example sets binary_location. You can download Chrome for Testing with npx @puppeteer/browsers install chrome@stable.
Also keep in mind:
- The credentials sit in plain text in
background.js. Keep the folder private and out of version control. - For several sessions, create one extension folder per proxy line.
- Check the exit IP on the first page before you start real work, in case the first request went out before the extension applied the setting.
Option 3: a tool with built-in proxy authentication
If you can choose your automation tool, Playwright takes the username and password as launch options, and Puppeteer handles them with page.authenticate(). Both avoid the workarounds on this page.
What about Selenium Wire?
Selenium Wire was the usual answer for years, but its repository was archived in January 2024 and the project is no longer maintained. Avoid it for new work. If you depend on a fork, check how actively it is maintained before you rely on it.
Our Chrome extension
The FreshProxies Chrome extension is built for everyday browsing, with profiles you switch by hand. It isn't designed for automated Selenium runs, so use one of the options above instead. If something still fails, troubleshooting helps you tell proxy errors from website errors.