【问题标题】:Java- Why Selenium Chrome webdiver is using my real IP adress instead of the proxyJava-为什么 Selenium Chrome webdriver 使用我的真实 IP 地址而不是代理
【发布时间】:2020-03-31 20:30:12
【问题描述】:

我正在尝试使用代理浏览 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();
}

【问题讨论】:

  • @DebanjanB 谢谢,但你的代码没有解决我的问题我发布了一个仅通过使用参数就可以工作的答案谢谢

标签: java selenium selenium-webdriver proxy selenium-chromedriver


【解决方案1】:

根据Java DocsMutableCapabilities 中的setCapability() 方法定义为:

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;博士

Capabilities & ChromeOptions

【讨论】:

    【解决方案2】:

    以下代码解决了我的问题

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-08
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      相关资源
      最近更新 更多