Python Selenium/PhantomJS 引发错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29869757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:06:58  来源:igfitidea点击:

Selenium/PhantomJS raises error

pythonseleniumbrowserphantomjs

提问by Milano

I'm trying to run PhantomJSdriver in Pythonbut I'm getting error. I've read that I should pass the whole path as an argument but it didn't help.

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

Here is the code:

这是代码:

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

Do you know what am I doing wrong?

你知道我做错了什么吗?

采纳答案by WEI YU

Make the path in raw string, add 'r': 

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

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

回答by Malik Brahimi

For simplicity's sake place the executable in the same directory as your script:

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

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

回答by Allan Cascante

For me none of the above solved the problem; I found the code:

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

driver = webdriver.PhantomJS()

Will only work for root...

仅适用于root...

回答by Sameer Narkhede

For me, this works fine and also don't have to specify path for PhantomJS You need to install pip install phantomjs-binary binary package using pip

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

pip install phantomjs-binary

it will download a package of approx 60MB. and contains PhantomJS for windows, mac, and Linux as per the running environment. after that, you can use it

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

from selenium import webdriver
from phantomjs_bin import executable_path

driver = webdriver.PhantomJS(executable_path=executable_path)