You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中配置Selenium Chrome&Firefox WebDriver的HTTPS代理

Fixing Chrome Proxy Setup in Selenium (No More Confusing Snippets!)

Hey there, I’ve fought through the same messy proxy setup issues with Selenium and Chrome—those scattered, half-working code snippets online are so frustrating. Let’s walk through the reliable methods that finally got my proxies working, whether you’re dealing with a basic proxy or one that requires username/password auth.

1. Basic (No Authentication) Proxy Setup

First off, most people mess up the argument name: it’s --proxy-server, not --proxy. That’s one of the most common mistakes! Here’s the correct code:

from selenium import webdriver

proxy_ip_and_port = "123.45.67.89:8080"  # Replace with your proxy details

chrome_options = webdriver.ChromeOptions()
# Important: Use --proxy-server, and specify the protocol (http/https)
chrome_options.add_argument(f'--proxy-server=http://{proxy_ip_and_port}')

# Launch the driver with configured options
driver = webdriver.Chrome(options=chrome_options)

If your proxy uses HTTPS, just swap http:// with https:// in the argument.

2. Proxy with Username/Password Authentication

This is where most old snippets fail—Chrome dropped support for the --proxy-auth argument a while back. You have two solid options here:

Option A: Use a Chrome Extension (Reliable for All Cases)

We’ll create a tiny Chrome extension that handles proxy authentication automatically.

First, create two files in a folder:

  1. manifest.json (extension configuration):
{
  "version": "1.0.0",
  "manifest_version": 3,
  "name": "Proxy Auth Helper",
  "permissions": ["proxy", "tabs", "storage", "<all_urls>"],
  "background": {
    "service_worker": "background.js"
  },
  "minimum_chrome_version": "100"
}
  1. background.js (handles proxy setup and auth):
// Replace these with your proxy details
const PROXY_HOST = "123.45.67.89";
const PROXY_PORT = 8080;
const PROXY_USER = "your_username";
const PROXY_PASS = "your_password";

// Set up the proxy
const proxyConfig = {
  mode: "fixed_servers",
  rules: {
    singleProxy: {
      scheme: "http",
      host: PROXY_HOST,
      port: PROXY_PORT
    },
    bypassList: ["localhost"] // Skip proxy for local sites
  }
};

chrome.proxy.settings.set({ value: proxyConfig, scope: "regular" }, () => {});

// Handle authentication requests
chrome.webRequest.onAuthRequired.addListener(
  (details, callback) => {
    callback({
      authCredentials: {
        username: PROXY_USER,
        password: PROXY_PASS
      }
    });
  },
  { urls: ["<all_urls>"] },
  ["blocking"]
);

Zip these two files into a .zip archive (e.g., proxy_auth_extension.zip). Then load it in your Selenium code:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
# Load the extension zip file
chrome_options.add_extension("proxy_auth_extension.zip")

driver = webdriver.Chrome(options=chrome_options)

Option B: Use Chrome DevTools Protocol (No Extension Needed)

If you don’t want to mess with extensions, use Selenium’s support for Chrome DevTools Protocol (CDP) to inject auth headers:

from selenium import webdriver
import base64

# Your proxy details
proxy_ip_and_port = "123.45.67.89:8080"
proxy_user = "your_username"
proxy_pass = "your_password"

chrome_options = webdriver.ChromeOptions()
# First set the proxy server
chrome_options.add_argument(f'--proxy-server=http://{proxy_ip_and_port}')

driver = webdriver.Chrome(options=chrome_options)

# Enable Network via CDP
driver.execute_cdp_cmd("Network.enable", {})

# Encode credentials for Proxy-Authorization header
auth_encoded = base64.b64encode(f"{proxy_user}:{proxy_pass}".encode()).decode()
driver.execute_cdp_cmd(
    "Network.setExtraHTTPHeaders",
    {"headers": {"Proxy-Authorization": f"Basic {auth_encoded}"}}
)

Key Notes to Avoid Headaches

  • Stop using DesiredCapabilities: Selenium has moved away from this in favor of ChromeOptions—stick with the latter for modern versions.
  • Double-check proxy format: Make sure your proxy IP/port is correct, and you’re using the right protocol (http/https) in the --proxy-server argument.
  • Version matching: Ensure your Selenium version is compatible with your Chrome version (e.g., Selenium 4.x works best with Chrome 100+).

内容的提问来源于stack exchange,提问作者Ryflex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:38:37