Selenium无法连接到GhostDriver(但有时只能)

时间:2014-07-04 17:38:33

标签: python selenium phantomjs ghostdriver

我在Python中使用Selenium和PhantomJS设置了一个简单的webscraping脚本。我总共得到了大约200个网址。该脚本在大约20-30个URL之后首先运行正常(它可以更多/更少,因为它在失败时似乎是随机的并且与任何特定URL无关)我在python中得到以下错误:

selenium.common.exceptions.WebDriverException: Message: 'Can not connect to GhostDriver'

我的ghostdriver.log:

PhantomJS is launching GhostDriver...
[ERROR - 2014-07-04T17:27:37.519Z] GhostDriver - main.fail - {"message":"Could not start Ghost Driver","line":82,"sourceId":140692115795456,"sourceURL":":/ghostdriver/main.js","stack":"Error: Could not start Ghost Driver\n    at :/ghostdriver/main.js:82","stackArray":[{"sourceURL":":/ghostdriver/main.js","line":82}]}

我已经搜索了大部分关于SO的问题似乎是他们甚至无法运行单个网址。我在脚本中间发现错误的唯一问题是this one,答案是将phantomjs升级到最新版本,我已经完成了。另一个答案只是说再次尝试该URL并不是一个好的解决方案,因为URL可能会再次失败。

我在python 2.7.6上的Linux Mint 17上运行phantomjs版本1.9.7和selenium版本2.42.1

for url in ['example.com/1/', 'example.com/2/', 'example.com/3/', .. , ..]:
    user_agent = 'Chrome'
    dcap = dict(DesiredCapabilities.PHANTOMJS)
    dcap['phantomjs.page.settings.userAgent'] = user_agent
    driver = webdriver.PhantomJS(executable_path='/usr/bin/phantomjs', desired_capabilities=dcap)
    driver.get(url)

1 个答案:

答案 0 :(得分:8)

我遇到同样的问题要解决它我从源代码安装了phantomjs

For Linux (Debian):
sudo apt-get update
sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev libxft-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.9
./build.sh

For Mac os:
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.9
./build.sh

对于其他系统,请检查以下链接 http://phantomjs.org/build.html

Optional :
cd bin
chmod +x phantomjs
cp phantomjs /usr/bin/

我想通了,因为当我读到我的ghostdriver.log文件时,它说。

[ERROR - 2014-09-04T19:33:30.842Z] GhostDriver - main.fail - {"message":"Could not start Ghost Driver","line":82,"sourceId":140145669488128,"sourceURL":":/ghostdriver/main.js","stack":"Error: Could not start Ghost Driver\n    at :/ghostdriver/main.js:82","stackArray":[{"sourceURL":":/ghostdriver/main.js","line":82}]}

我确信必须有一些丢失的文件,它必须用于某些边缘情况。所以我决定从源码开始构建它现在正常工作。