Java - 具有代理设置的Selenium PhantomJS RemoteDriver无法正常工作

时间:2014-12-26 10:44:44

标签: java selenium phantomjs

我想将Selenium的RemoteDriver与PhantomJS一起使用代理+多线程。我的问题是RemoteDriver没有在我的设置中使用给定的代理。

我尝试了以下两种设置,两次都可以在PhantomJS响应中看到正确的代理,但它仍在使用我的本地IP。

在CLI中,我使用:phantomjs --webdriver=4444

启动PhantomJS

首先设置:

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(proxyHost);
DesiredCapabilities cap = new DesiredCapabilities().phantomjs();
cap.setCapability(CapabilityType.PROXY, proxy);

try {
    this.driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
} catch (MalformedURLException e) {
    e.printStackTrace();
}
this.driver.get("http://whatismyipaddress.com/");

正如PhantonJS的回应所说:

  

[INFO - 2014-12-26T10:29:42.902Z]会议   [1ade9c30-8cea-11e4-952c-61f83d60a6ec] - CONSTRUCTOR - 所需   功能:   { “平台”: “ANY”, “browserName”: “phantomjs”, “代理”:{ “HTTPPROXY”: “173.234.249.114:3128","noProxy":null,"socksUsername":null,"ftpProxy”:空, “socksPassword”:空, “sslProxy”:空, “proxyAutoconfigUrl”:空 “的proxyType”: “手动”, “阶级”: “org.openqa.selenium.Proxy”, “自动检测”:假的,“socksProxy “:空,” hCode “:1338671111},” 版本 “:”“}   [INFO - 2014-12-26T10:29:42.902Z]会议   [1ade9c30-8cea-11e4-952c-61f83d60a6ec] - 构造函数 - 谈判   功能:   { “browserName”: “phantomjs”, “版本”: “1.9.0”, “驱动程序名”: “ghostdriver”, “driverVersion”: “1.0.3”, “平台”: “Linux的未知-64”,” javascriptEnabled “:真实的,” takesScreenshot “:真实的,” handlesAlerts “:假的,” databaseEnabled “:假的,” locationContextEnabled “:假的,” applicationCacheEnabled “:假的,” browserConnectionEnabled “:假的,” cssSelectorsEnabled “:真实的,” webStorageEnabled” :假的, “旋转”:假的, “acceptSslCerts”:假的, “nativeEvents”:真实的, “代理”:{ “HTTPPROXY”:“173.234.249.114:3128","noProxy":null,"socksUsername":null, “ftpProxy”:空, “socksPassword”:空, “sslProxy”:空, “proxyAutoconfigUrl”:空 “的proxyType”: “手动”, “阶级”: “org.openqa.selenium.Proxy”, “自动检测”:假 “socksProxy”:空, “hCode”:1338671111}}   [INFO - 2014-12-26T10:29:42.902Z] SessionManagerReqHand -   _postNewSessionCommand - 创建的新会话:1ade9c30-8cea-11e4-952c-61f83d60a6ec

第二次设置:

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(proxyHost);
DesiredCapabilities cap = new DesiredCapabilities().phantomjs();
ArrayList<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--proxy=proxy");
cliArgsCap.add("--proxy-type=http");
cap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, proxy);

try {
    this.driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
} catch (MalformedURLException e) {
    System.out.println("nope");
    e.printStackTrace();
}
this.driver.get("http://whatismyipaddress.com/");

作为回应PhantomJS说:

  

[INFO - 2014-12-26T10:36:38.259Z]会议   [12710f00-8ceb-11e4-952c-61f83d60a6ec] - CONSTRUCTOR - 所需   功能:   { “平台”: “ANY”, “phantomjs.cli.args”:{ “HTTPPROXY”:“173.234.249.114:3128","noProxy":null,"socksUsername":null,"ftpProxy":null,"socksPassword “:空,” sslProxy “:空,” proxyAutoconfigUrl “:空” 的proxyType “:” 手动”, “阶级”: “org.openqa.selenium.Proxy”, “自动检测”:假的, “socksProxy”:空, “hCode”:2130507747}, “browserName”: “phantomjs”, “版本”: “”}   [INFO - 2014-12-26T10:36:38.259Z]会议   [12710f00-8ceb-11e4-952c-61f83d60a6ec] - 构造函数 - 谈判   功能:   { “browserName”: “phantomjs”, “版本”: “1.9.0”, “驱动程序名”: “ghostdriver”, “driverVersion”: “1.0.3”, “平台”: “Linux的未知-64”,” javascriptEnabled “:真实的,” takesScreenshot “:真实的,” handlesAlerts “:假的,” databaseEnabled “:假的,” locationContextEnabled “:假的,” applicationCacheEnabled “:假的,” browserConnectionEnabled “:假的,” cssSelectorsEnabled “:真实的,” webStorageEnabled” :假的, “旋转”:假的, “acceptSslCerts”:假的, “nativeEvents”:真实的, “代理”:{ “的proxyType”: “直接”}}   [INFO - 2014-12-26T10:36:38.259Z] SessionManagerReqHand -   _postNewSessionCommand - 新会话创建时间:12710f00-8ceb-11e4-952c-61f83d60a6ec

正如您所看到的那样,两次都使用了正确的代理地址("httpProxy":"173.234.249.114:3128"),但是whatismyipaddress.com正在报告我自己的地址。

当我使用GhostDriver中的PhantomJSDriver时,我可以使用常规org.openqa.selenium.Proxy,但我无法按照here所述完成多线程。使用CLI和RemoteDriver中的PhantomJS流程,至少可以使用多线程。

感谢您的任何建议!抱歉长文:)

--- UPDATE ---

刚刚发现,如果我使用--proxy直接在CLI中启动PhontomJS,那么当我使用RemoteDriver连接时,它会使用给定的代理。对我来说,一个选择就是现在只需用固定代理手动启动多个PhantonJS进程。

0 个答案:

没有答案