Set the HTTP proxy before you start Chrome
Pass --proxy-server to ChromeOptions and start a fresh Selenium browser with those options. The value is the address of the proxy server. For IPMusk Rotating Residential, use http://gate.ipmusk.com:31212. For a Static ISP or Datacenter proxy, use the host and port assigned to your address.
An HTTP proxy can carry a request to an HTTPS site. Chrome asks the proxy for a tunnel to the destination, then establishes TLS with the destination through that tunnel. The http scheme in the option below describes the connection to the proxy. Use the HTTPS URL of your test page for driver.get().
Install the selenium package in the Python environment that runs your tests. For a quick check, use a small HTTPS page you own and set PROXY_TEST_URL to its address. If your proxy prompts for a login, enter the full proxy username and password in the browser. The gateway value stays the same either way.
import os
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=http://gate.ipmusk.com:31212")
with webdriver.Chrome(options=options) as driver:
driver.set_page_load_timeout(60)
driver.get(os.environ["PROXY_TEST_URL"])
print(driver.title)For an interactive check, give yourself time to enter the proxy login. Run the authenticated example below when a test needs to run without somebody at the keyboard.
Supply the proxy login in an unattended test
Chrome's manual proxy setting accepts the proxy address and ignores credentials embedded in that address. In a dedicated test browser, a small Manifest V3 extension can supply the login when the proxy challenges the request. Use Chrome for Testing with the example below. Google removed the --load-extension launch switch from regular branded Chrome starting in version 137.
Set CHROME_FOR_TESTING_BIN to the path of the browser executable. Set PROXY_USERNAME and PROXY_PASSWORD in the environment that runs the script. If your provider uses a generated username for a country or session, copy the whole value, including its suffix. Set PROXY_TEST_URL to a small HTTPS page on your own test host that loads without redirecting to a different hostname.
The extension registers for that test hostname and checks that the challenge comes from the configured proxy host and port. A sign-in challenge from the website receives no credentials from the extension. If the proxy challenges the same request again, the extension cancels it so a typo cannot keep the navigation stuck in a login loop.
import json
import os
from pathlib import Path
from tempfile import TemporaryDirectory
from urllib.parse import urlsplit
from selenium import webdriver
host, port = "gate.ipmusk.com", 31212
test_url = os.environ["PROXY_TEST_URL"]
target = urlsplit(test_url)
if target.scheme != "https" or not target.hostname:
raise ValueError("Use the HTTPS URL of your test page")
scope = f"https://{target.hostname}/*"
config = {
"host": host,
"port": port,
"scope": scope,
"credentials": {
"username": os.environ["PROXY_USERNAME"],
"password": os.environ["PROXY_PASSWORD"],
},
}
manifest = {
"manifest_version": 3,
"name": "Test proxy login",
"version": "1.0",
"permissions": ["webRequest", "webRequestAuthProvider"],
"host_permissions": [scope],
"background": {"service_worker": "worker.js"},
}
worker = "const config = " + json.dumps(config) + ";\n" + """
const attempted = new Set();
chrome.webRequest.onAuthRequired.addListener(
(details, reply) => {
const { challenger, isProxy, requestId } = details;
if (!isProxy || challenger.host !== config.host ||
challenger.port !== config.port) {
reply({});
return;
}
if (attempted.has(requestId)) {
reply({ cancel: true });
return;
}
attempted.add(requestId);
reply({ authCredentials: config.credentials });
},
{ urls: [config.scope] },
["asyncBlocking"],
);
"""
with TemporaryDirectory() as folder:
extension = Path(folder)
(extension / "manifest.json").write_text(
json.dumps(manifest), encoding="utf-8"
)
(extension / "worker.js").write_text(worker, encoding="utf-8")
options = webdriver.ChromeOptions()
options.binary_location = os.environ["CHROME_FOR_TESTING_BIN"]
options.add_argument(f"--proxy-server=http://{host}:{port}")
options.add_argument(f"--load-extension={extension}")
with webdriver.Chrome(options=options) as driver:
driver.set_page_load_timeout(30)
driver.get(test_url)
print(driver.title)The worker file contains the proxy login while the test runs. The temporary directory is cleaned up on normal exit. Keep it out of source control, debug captures and CI artifacts. Use credentials reserved for the test.
Check the exit address in the same browser
Confirm the expected title or an element your page always renders. That checks that Selenium reached the right content. To verify the route, have the same test page show the client address recorded by your server or trusted ingress. The address it sees should be the proxy's exit address. The gateway hostname is the point you connect to, so resolving its DNS tells you little about the exit used for a request.
For example, a checkout team can open a staging health page twice from the same runner. On the first run, record the address from a fresh browser without the test's proxy setting. On the second, enable the setting and compare the address at the same endpoint. Keep the host and page the same between runs. If the runner already uses a company proxy or VPN, record that in the baseline.
Chromium skips the proxy for loopback URLs by default. A page at localhost can still load even if the configured proxy is down. Use a test hostname reachable from the proxy and compare what your server actually recorded. On a remote Selenium Grid, make sure the browser node itself can reach the proxy and your test host.
Tell a startup error from a proxy failure
When the browser starts but navigation fails, run one request with curl from the same runner through the same gateway to the same test URL. If curl works with the same credentials, check how Chrome received the proxy setting and whether the authentication handler saw the challenge. If both fail, investigate the connection or proxy account before changing the Selenium test.
A destination can return its own sign-in page after proxy authentication succeeds. If the route check works but the page test still fails, inspect the destination's access logs or the test account's permissions. Keep the connection check small so you can identify the first failing step before you bring the wider test suite back in.
| What you see | What to check |
|---|---|
| WebDriver fails before a tab opens | Check the browser binary path and driver compatibility on the machine that starts Chrome. |
| Chrome keeps prompting or the proxy sends 407 | Check the full username and password, then confirm the extension loaded in Chrome for Testing and the test hostname matches its scope. |
| Navigation reports a proxy connection error | Check that the runner can reach the proxy host and port and that you chose the HTTP endpoint. |
| The page opens with the baseline address | Check for a bypass rule, a loopback URL or a browser session created without the intended ChromeOptions. |
