Java PhantomjsDriver 不适用于 Windows 和 Linux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20941518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PhantomjsDriver not working on both Windows and Linux
提问by bsg
I have an application that uses Selenium Webdriver to get some information from a site. It works fine with FirefoxDriver and ChromeDriver, but when I tried to switch to PhantomJSDriver, I encountered some difficulties.
我有一个应用程序,它使用 Selenium Webdriver 从站点获取一些信息。它适用于 FirefoxDriver 和 ChromeDriver,但是当我尝试切换到 PhantomJSDriver 时,我遇到了一些困难。
- On a Windows machine , it starts normally, then immediately begins spitting out the following lines over and over again:
- 在 Windows 机器上,它正常启动,然后立即开始一遍又一遍地吐出以下几行:
Jan 05, 2014 7:28:43 PM org.apache.http.impl.client.DefaultRequestDirector tryEx ecute INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
2014 年 1 月 5 日下午 7:28:43 org.apache.http.impl.client.DefaultRequestDirector tryEx ecute INFO:处理请求时捕获 I/O 异常 (org.apache.http.NoHttpResponseException):目标服务器未能响应
This is repeated probably several hundred times for about 10 minutes until it finally loads the page; sometimes it doesn't even manage to load it at all.
这可能会重复数百次大约 10 分钟,直到它最终加载页面;有时它甚至根本无法加载它。
- On a Linux machine, it tries to start, then returns the following:
- 在 Linux 机器上,它尝试启动,然后返回以下内容:
Exception in thread "thread1" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. Build info: version: 'unknown', revision: 'unknown', time: 'unknown' System info: host: 'pangolin', ip: '128.238.32.20', os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-39-generic', java.version: '1.7.0' Driver info: driver.version: PhantomJSDriver at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:111) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:115) at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:107) at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:96) Caused by: org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start. Build info: version: 'unknown', revision: 'unknown', time: 'unknown' System info: host: 'pangolin', ip: '128.238.32.20', os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-39-generic', java.version: '1.7.0' Driver info: driver.version: PhantomJSDriver at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:165) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:62) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527) ... 7 more Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:16050/status] to be available after 20002 ms at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:104) at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:163) ... 9 more Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:143) at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:79) ... 10 more Caused by: java.util.concurrent.TimeoutException at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:258) at java.util.concurrent.FutureTask.get(FutureTask.java:119) at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130) ... 11 more
线程“thread1”org.openqa.selenium.remote.UnreachableBrowserException 中的异常:无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败。构建信息:版本:'unknown',修订:'unknown',时间:'unknown' 系统信息:host:'pangolin',ip:'128.238.32.20',os.name:'Linux',os.arch:' amd64', os.version: '2.6.32-39-generic', java.version: '1.7.0' 驱动程序信息: driver.version: PhantomJSDriver at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java :548) 在 org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216) 在 org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:111) 在 org.openqa.selenium。remote.RemoteWebDriver.(RemoteWebDriver.java:115) 在 org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:107) 在 org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:96) Caused org.openqa.selenium.WebDriverException:等待驱动程序服务器启动超时。构建信息:版本:'unknown',修订:'unknown',时间:'unknown' 系统信息:host:'pangolin',ip:'128.238.32.20',os.name:'Linux',os.arch:' amd64', os.version: '2.6.32-39-generic', java.version: '1.7.0' Driver info: driver.version: PhantomJSDriver at org.openqa.selenium.remote.service.DriverService.start(DriverService .java:165) 在 org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.
What am I doing wrong? I've read a lot about how Phantomjs is so much faster than the other drivers, and would really like to use it, but if it takes 10 minutes to load each page, that's obviously not feasible.
我究竟做错了什么?我已经阅读了很多关于 Phantomjs 如何比其他驱动程序快得多的内容,并且真的很想使用它,但是如果加载每个页面需要 10 分钟,那显然是不可行的。
I am running Selenium WebDriver version 2.38.0 and Phantomjs version 1.9.2.
我正在运行 Selenium WebDriver 版本 2.38.0 和 Phantomjs 版本 1.9.2。
Thank you very much in advance, bsg
非常感谢您, bsg
EDITJust to clarify, I don't think this has anything to do with my code; the errors on Linux are being thrown on the line where I try to start the PhantomJS driver, below.
编辑只是为了澄清,我认为这与我的代码没有任何关系;Linux 上的错误在我尝试启动 PhantomJS 驱动程序的那一行被抛出,如下所示。
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
phantombinary//"/home/p/phantomjs-1.9.2-linux-x86_64/bin/phantomjs"
);
// Launch driver (will take care and ownership of the phantomjs process)
WebDriver driver = new PhantomJSDriver(caps);
System.out.println("starting driver");
回答by Zach
org.openqa.selenium.remote.UnreachableBrowserException
clearly something to do with Phantom. On Windows ensure Phantom is running (Set Environment variable and add PATH)
Check that Remote Machine hub address is correct and you should be able to run phantomjs.
org.openqa.selenium.remote.UnreachableBrowserException
显然与Phantom有关。在 Windows 上确保 Phantom 正在运行(设置环境变量并添加 PATH) 检查远程机器集线器地址是否正确,您应该能够运行 phantomjs。
Note: Selenium Server must be running on the desired remote machine.
注意:Selenium Server 必须在所需的远程机器上运行。
DesiredCapabilities phantomBeast = DesiredCapabilities.phantomjs();
try {
webDriverInstance = new RemoteWebDriver(new URL(hubUrl), phantomBeast);
} catch (Exception e) {
//Do something
}
回答by user3948977
If it is still not working for you on Linux, try below piece of code, it is working for me on Mac.
如果它在 Linux 上仍然不适合你,请尝试下面的代码,它在 Mac 上对我有用。
capabilities.setCapability("phantomjs.binary.path", "path of phantom binary/phantomjs")