Selenium / PhantomJS引发错误

时间:2015-04-25 19:32:06

标签: python selenium browser phantomjs

我正在尝试在 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')

ERROR:

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

你知道我做错了什么吗?

3 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:0)

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

driver = webdriver.PhantomJS()

只适用于root ...

答案 2 :(得分:0)

对我来说,这很好用,也不必为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)