【问题标题】:Selenium can't connect to GhostDriver (but only sometimes)Selenium 无法连接到 GhostDriver(但有时)
【发布时间】:2014-07-04 17:38:32
【问题描述】:

我在 Python 中使用 Selenium 和 PhantomJS 设置了一个简单的网页抓取脚本。我总共要抓取大约 200 个 URL。该脚本首先运行良好,然后在大约 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 的大多数问题似乎是它们甚至无法运行单个 URL。我发现脚本中间出现错误的唯一另一个问题是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)

【问题讨论】:

  • @PadraicCunningham:错字。固定。
  • 你是如何安装 phantomjs 的?
  • 通过phantomjs.org/download.html的二进制文件
  • 我也看到了这个问题。关于是否/如何修复它的任何更新都会很棒。

标签: python selenium phantomjs ghostdriver


【解决方案1】:

我遇到了同样的问题来解决它我从源代码安装了 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}]}

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

【讨论】:

  • 为 OSX 构建 Phantom 很痛苦。它要求 QT 库。
猜你喜欢
  • 2013-07-23
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-31
  • 2013-01-14
  • 2017-10-11
相关资源
最近更新 更多