我将代理设置为Chrome驱动程序
ChromeOptions optionsMap = new ChromeOptions();
optionsMap.addArguments("--headless");
optionsMap.addArguments("--user-agent=Mozilla/5.0 (X11; Linux x86_64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36");
Proxy proxy = new Proxy();
proxy.setProxyType(ProxyType.MANUAL);
proxy.setHttpProxy(proxyValue);
optionsMap.setCapability(CapabilityType.PROXY, proxy);
但是流量来自本地IP地址。我希望流量来自代理。您能解决这个问题吗?为了测试,我将chrome驱动程序代理设置为其他IP而不是本地地址,然后运行网站https://icanhazip.com/来了解IP。该网站仍显示本地IP。
我使用以下代码构建了chrome驱动程序
String driverAbsPath = new File(seleniumChromeDriverPath).getAbsolutePath().replace("/~/", "/");
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File(driverAbsPath))
.usingAnyFreePort()
.build();
optionsMap.merge(chromecapabilities);
driver = new ChromeDriver(service, optionsMap);
尝试将IP地址添加到代理端的X-Forwarded-For标头中:
proxy.addHeader("X-Forwarded-For", "1.2.3.4")