Java WebDriver PhantomJS 启动异常

直接看异常吧:

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: '3.2.0', revision: '8c03df6b79', time: '2017-02-23 10:51:31 +0000'
System info: host: 'tjtx128-0-137.58os.org', ip: '10.128.0.137', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-327.el7.x86_64', java.version: '1.8.0_66'
Driver info: driver.version: PhantomJSDriver
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:625)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:244)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:144)
        at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:116)
        at com.xxx.component.PhantomJSDriver2.createPhantomJSDriver(PhantomJSDriver2.java:37)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: The process has not exited yet therefore no result is available ...
        at org.apache.commons.exec.DefaultExecuteResultHandler.getException(DefaultExecuteResultHandler.java:76)
        at org.openqa.selenium.os.UnixProcess.checkForError(UnixProcess.java:174)
        at org.openqa.selenium.os.CommandLine.checkForError(CommandLine.java:183)
        at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:179)
        at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:168)
        at org.openqa.selenium.phantomjs.PhantomJSCommandExecutor.execute(PhantomJSCommandExecutor.java:78)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:604)
        ... 12 more

这种问题单看日志是没什么东西的,实际都是 phantomjs 没有正常启动导致的,处理方式可以直接从项目里找到代理命令后执行:

./phantomjs --webdriver=58001 --webdriver-logfile=/data/logs/phantomjs.log

输出可能是这样的:

Unable to open file '/data/logs/phantomjs.log'

  phantomjs://platform/fs.js:79 in open
  phantomjs://platform/logger.js:95 in addLogFile
  phantomjs://platform/config.js:76 in apply
  phantomjs://platform/config.js:98 in init

很明显是文件权限不足了,通过 chmod 命令修改即可。

还一个可能是启动正常、日志正常,但是 java 进程仍然报异常,这个可能是网络导致的,java 进程启动 phantomjs 进程后会请求 http://localhost:port/status 地址进行状态监测,如果没有监测到也会认为启动失败,例如:

[~]# curl http://localhost:58001
curl: (6) Couldn't resolve host 'localhost'

这种情况修改 /etc/hosts 添加 127.0.0.1 localhost 的映射就可以了。

2 comments

  1. 大佬你好,我最近做了一个用PhantomJS驱动的Javaweb项目,目的是从网站上获取文本存入数据库,但是发布的服务器使用的是Linux,我在idea用的是PhantomJS.exe来运行的,该怎么修改驱动配置能让它在Linux环境下运行

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注