使用Selenium PhantomJS代理

时间:2015-08-04 00:37:37

标签: java selenium-webdriver proxy phantomjs ghostdriver

我刚刚添加了与代理相关的两行

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability("takesScreenshot", true);
caps.setCapability("screen-resolution", "1280x1024");
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\\Users\\USERNAME\\Downloads\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe");
caps.setCapability("proxy", "127.0.0.1:1024"); //added this line
caps.setCapability("proxy-type", "socks"); //and this line
Logger.getLogger(PhantomJSDriverService.class.getName()).setLevel(Level.OFF);

driver = new PhantomJSDriver(caps);

但是现在我运行程序时出现以下错误

[INFO  - 2015-08-04T00:34:45.924Z] GhostDriver - Main - running on port 15821
[ERROR - 2015-08-04T00:34:46.845Z] RouterReqHand - _handle.error - {"stack":"\tat _getProxySettingsFromCapabilities (:/ghostdriver/session.js:131:41)\n\tat Session (:/ghostdriver/session.js:165:62)\n\tat _postNewSessionCommand (:/ghostdriver/request_handlers/session_manager_request_handler.js:75:49)\n\tat _handle (:/ghostdriver/request_handlers/session_manager_request_handler.js:44:35)\n\tat _handle (:/ghostdriver/request_handlers/router_request_handler.js:70:37)","line":131,"sourceURL":":/ghostdriver/session.js"}

  :262 in error
[INFO  - 2015-08-04T00:34:46.861Z] ShutdownReqHand - _handle - About to shutdown
Aug 03, 2015 8:34:47 PM org.openqa.selenium.os.UnixProcess$SeleniumWatchDog destroyHarder
INFO: Command failed to close cleanly. Destroying forcefully (v2). org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@7e1d83ab
org.openqa.selenium.UnsupportedCommandException: TypeError - undefined is not an object (evaluating 'proxyCapability["proxyType"].toLowerCase')

我做错了什么?

1 个答案:

答案 0 :(得分:2)

我已经弄明白了,这就是那个遇到同样问题的人的方式

ArrayList<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--proxy=127.0.0.1:1024");
// cliArgsCap.add("--proxy-auth=username:password");
cliArgsCap.add("--proxy-type=socks5");

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability("takesScreenshot", true);
caps.setCapability("screen-resolution", "1280x1024");
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\\Users\\USER\\Downloads\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe");

caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);

Logger.getLogger(PhantomJSDriverService.class.getName()).setLevel(Level.OFF);

driver = new PhantomJSDriver(caps);