【问题标题】:Selenium/PhantomJS raises errorSelenium/PhantomJS 引发错误
【发布时间】:2015-07-04 08:56:09
【问题描述】:

我正在尝试在 Python 中运行 PhantomJS 驱动程序,但出现错误。我读过我应该将整个路径作为参数传递,但它没有帮助。

代码如下:

from selenium import webdriver

# driver = webdriver.Chrome('D:\Python_projects\chromedriver_win32/chromedriver.exe') # this works
driver = webdriver.PhantomJS(executable_path='D:\Python\phantomjs-2.0.0-windows\bin\phantomjs.exe')

错误:

Traceback (most recent call last):
  File "path to script", line 8, in <module>
    driver = webdriver.PhantomJS(executable_path='D:\Python\phantomjs-2.0.0-windows\bin\phantomjs.exe')
  File "C:\Python27\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py", line 50, in __init__
    self.service.start()
  File "C:\Python27\lib\site-packages\selenium\webdriver\phantomjs\service.py", line 75, in start
    raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
selenium.common.exceptions.WebDriverException: Message: Unable to start phantomjs with ghostdriver.
Screenshot: available via screen

你知道我做错了什么吗?

【问题讨论】:

  • @MalikBrahimi 文档说应该给出可执行文件的路径 - selenium.googlecode.com/svn/trunk/docs/api/py/…
  • 为什么不在系统路径中添加包含可执行文件的目录?
  • 无法在 Windows 上使用 Selenium 2.44.0 和 PhantomJS 2.0.0 进行复制。

标签: python selenium browser phantomjs


【解决方案1】:

在原始字符串中创建路径,添加'r':

driver = webdriver.PhantomJS(executable_path=r'D:\Python\phantomjs-2.0.0-windows\bin\phantomjs.exe')

【讨论】:

  • 这对我有用,虽然将 phantomjs.exe 移动到脚本文件夹也可以,但它似乎比实际获取要接受的路径要少。
  • 它也可以正确地转义反斜杠(即用 C:\\...\\... 代替 C:\...\... 等等等等
【解决方案2】:

为简单起见,将可执行文件放在与您的脚本相同的目录中:

driver = webdriver.PhantomJS() # now there's no need for a path

【讨论】:

  • 谢谢!我不知道为什么即使指定了正确的路径它也不起作用...将 phantomjs.exe 复制到我的 python 文件夹有效。
  • 这就是答案!
【解决方案3】:

对我来说,以上都没有解决问题;我找到了代码:

driver = webdriver.PhantomJS()

仅适用于 root...

【讨论】:

    【解决方案4】:

    对我来说,这很好用,也不必为 PhantomJS 指定路径 您需要使用 pip 安装 pip install phantomjs-binary 二进制包

    pip install phantomjs-binary
    

    它将下载一个大约 60MB 的包。 并根据运行环境包含适用于 windows、mac 和 Linux 的 PhantomJS。 之后就可以使用了

    from selenium import webdriver
    from phantomjs_bin import executable_path
    
    driver = webdriver.PhantomJS(executable_path=executable_path)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      相关资源
      最近更新 更多