Selenium配置代理后仍显示本地IP的问题咨询
Alright, let's dig into why your proxy setup isn't working as expected. I've run into this exact issue before, so here are the most likely culprits and how to fix them:
1. You only configured HTTP proxy, but your requests use HTTPS
Your current code only sets the http field in proxy.manual(), but if you're accessing HTTPS websites (which most sites are these days), Chrome will default to your home IP because you didn't specify a proxy for HTTPS traffic. Update your code to include both protocols:
var driver = new webdriver.Builder() .withCapabilities(webdriver.Capabilities.chrome()) .setProxy(proxy.manual({ http: '103.87.16.2:80', https: '103.87.16.2:80' // Add this line for HTTPS requests })) .build();
2. Chrome is overriding your proxy with system/saved settings
Sometimes Chrome ignores Selenium's proxy configuration if it's set to use system proxy settings, or if you have a saved proxy profile from past use. To force Chrome to respect your code's proxy, start with a fresh, clean session:
var chromeOptions = new webdriver.ChromeOptions(); chromeOptions.addArguments('--no-proxy-server'); // Disable system proxy fallback chromeOptions.addArguments('--disable-extensions'); // Block VPN/proxy extensions that might interfere chromeOptions.addArguments('--incognito'); // Skip saved profile settings var driver = new webdriver.Builder() .forBrowser('chrome') .setChromeOptions(chromeOptions) .setProxy(proxy.manual({ http: '103.87.16.2:80', https: '103.87.16.2:80' })) .build();
3. The proxy itself isn't working
Before blaming your code, verify that 103.87.16.2:80 actually functions as a proxy:
- Manually set this proxy in Chrome's settings (Settings > System > Open your computer's proxy settings)
- Visit an IP-check site to see if your IP changes to the proxy's address
If it doesn't work manually, the proxy might be down, require authentication, or be a transparent proxy that doesn't hide your original IP.
4. Version mismatch between Selenium and ChromeDriver
Outdated or mismatched versions of Selenium WebDriver and ChromeDriver often cause proxy configuration bugs. Make sure:
- Your ChromeDriver version exactly matches your installed Chrome browser version (check Chrome's "About" page for your version, then grab the matching ChromeDriver)
- You're using the latest stable release of Selenium
5. The proxy requires authentication (and you didn't provide it)
If your proxy needs a username and password, Chrome will silently fall back to your home IP when it can't authenticate. For Chrome, you can handle this by adding authentication headers or using a dedicated proxy auth extension (since Selenium doesn't support proxy auth directly in all cases). Here's a quick header example:
var driver = new webdriver.Builder() .forBrowser('chrome') .setProxy(proxy.manual({ http: '103.87.16.2:80', https: '103.87.16.2:80' })) .build(); // Inject basic auth header (replace with your credentials) driver.executeScript(` const proxyAuth = 'Basic ' + btoa('your-username:your-password'); // Apply to all future fetch requests const originalFetch = fetch; window.fetch = (...args) => { args[1] = args[1] || {}; args[1].headers = args[1].headers || {}; args[1].headers['Proxy-Authorization'] = proxyAuth; return originalFetch(...args); }; `);
内容的提问来源于stack exchange,提问作者chosirai

