我正在尝试使用代理浏览Selenium,但它失败并显示我的公共IP
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\driverChrome.exe");
ChromeOptions option = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setHttpProxy("200.111.182.6:443");
option.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new ChromeDriver(option);
driver.get("https://whatismyipaddress.com");
driver.manage().window().maximize();
}
根据Java DocssetCapability()
来自MutableCapabilities的方法定义为:
setCapability
public void setCapability(java.lang.String key, java.lang.Object value)
因此,您必须使用setCapability("proxy", proxy);
而不是setCapability(CapabilityType.PROXY, proxy)
。因此,您的代码块将有效地:
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\driverChrome.exe");
ChromeOptions option = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setHttpProxy("200.111.182.6:443");
option.setCapability("proxy", proxy);
WebDriver driver = new ChromeDriver(option);
driver.get("https://whatismyipaddress.com");
driver.manage().window().maximize();
}
TL; DR
功能和Chrome选项
以下代码解决了我的问题
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\driverChrome.exe");
ChromeOptions option = new ChromeOptions();
option.addArguments("--proxy-server=http://51.38.22.57:8522");
WebDriver driver = new ChromeDriver(option);
driver.get("https://whatismyipaddress.com");
driver.manage().window().maximize();
}