如何从Java中的chromedriver获取代理设置?

时间:2019-07-11 09:12:36

标签: selenium-webdriver selenium-chromedriver

我有一段代码在创建chromedriver时自动获取代理,然后将代理配置到chromedriver中。有没有办法从chromedriver对象获取此代理?

这是我的代码:

public void getProxyFromDriver() {
    ChromeDriver driver = getDriver();
    //get the proxy from driver; 
    //ip, port, proxyType should be the same as the argument added when create the driver.
    MyProxy proxy;
}

private ChromeDriver getDriver() {
    System.setProperty("webdriver.chrome.driver", "chromeDriverPath");
    ChromeOptions options = new ChromeOptions();
    MyProxy proxy = getMyProxy(); //auto get proxy
    options.addArguments("--proxy-server=" + proxy.type + "://" + proxy.ip + ":" + proxy.port);
    return new ChromeDriver(options);
}

private MyProxy getMyProxy() {
    //Here is just an example, it is actually obtained randomly from the database.
    MyProxy myProxy = new MyProxy();
    myProxy.ip = "192.168.2.199";
    myProxy.port = 1090;
    myProxy.type = "SOCKS";
    return myProxy;
}

private class MyProxy {
    private String ip;
    private int port;
    private String type;
}

如果我不能直接获得代理,除了将代理添加为参数之外,还有什么好的建议吗?

更新:

我正在尝试通过以下代码获取代理:

Proxy proxy = (Proxy) driver.getCapabilities().getCapability("proxy");

但是我无法从返回的proxy对象中获取代理IP(或主机)以及端口和proxyType。

1 个答案:

答案 0 :(得分:0)

print("A" * n) for row in range(1, row - 1): print(" " * row # amount of spaces equals row index + "A" # 'A' char + " " * (n - row - 2) # amount of spaces between 'A' chars equals # length of row - row index - 2 + "A") # 'A' char print(" " * (n - 1) # amount of spaces on the start of last line # equals length of row - 1 + "A") # 'A' char 是普通Selenium Desired Capabilities

之一

enter image description here

因此您可以通过调用driver.getCapabilities()函数来访问它,例如:

proxy

请记住,该函数将返回org.openqa.selenium.Proxy的实例,因此您将需要使用上述类或确保您的Proxy proxy = (Proxy) driver.getCapabilities().getCapability("proxy"); 类与Selenium的兼容。