无法在 Windows 7 中使用 Python 启动 PhantomJS
Posted
技术标签:
【中文标题】无法在 Windows 7 中使用 Python 启动 PhantomJS【英文标题】:Can't launch PhantomJS with Python in Windows 7 【发布时间】:2016-04-22 18:16:04 【问题描述】:我正在尝试在我的 Windows 7 中将 PhantomJS 与 Python 一起使用,但无法正常工作! 这是我试图做的。
首先,我安装了 webdriver。在以下代码中,Firefox 正常打开,所以我相信 webdrive 已正确安装。
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
然后,我从官网下载了PhantomJS,解压后放入c:\Phantomjs。然后我将它添加到环境变量中。 里面有什么:
C:\Phantom\phantomjs-2.0.0-windows\bin
所以,当我在终端中键入“phantomjs”时,它会正常启动。但是下面的代码给了我一个错误:
from selenium import webdriver
browser = webdriver.PhantomJS( )
错误信息:
异常被忽略:> 回溯(最近一次通话最后): del 中的文件“c:Python34\lib\site-packages\selenium\webdriver\common\service.py”,第 136 行 文件“c:Python34\lib\site-packages\selenium\webdriver\common\service.py”,第 124 行,停止 AttributeError: 'NoneType' 对象没有属性 'close'
如果我稍微改变一下代码:
from selenium import webdriver
browser = webdriver.PhantomJS(executable_path='C:\Phantom\phantomjs-2.0.0-windows\bin\phantomjs.exe')
错误信息:
Traceback(最近一次调用最后一次):
文件“c:Python34\lib\site-packages\selenium\webdriver\common\service.py”,第 62 行,开始
stdout=self.log_file, stderr=self.log_file)
文件“c:Python34\lib\subprocess.py”,第 859 行,在 init
restore_signals, start_new_session)
文件“c:Python34\lib\subprocess.py”,第 1112 行,在 _execute_child 启动信息中)
FileNotFoundError: [WinError 2] 系统找不到指定的文件
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次):
文件“d:\test.py”,第 2 行,在
browser = webdriver.PhantomJS(executable_path='C:\Phantom\phantomjs-2.0.0-windows\bin\phantomjs')
文件“c:Python34\lib\site-packages\selenium\webdriver\common\service.py”,第 51 行,在 init self.service.start( )
文件“c:Python34\lib\site-packages\selenium\webdriver\common\service.py”,第 69 行,在 start os.path.basename(self.path),
self.start_error_message)
selenium.common.exceptions.WebDriverException:消息:“phantomjs”可执行文件需要在 PATH 中。
异常忽略于:del of
>
Traceback(最近一次调用最后一次):
文件“c:Python34\lib\site-packages\selenium\webdriver\common\service.py”,第 136 行,在 del self.stop( )
文件“c:Python34\lib\site-packages\selenium\webdriver\common\service.py”,第 117 行,停止
如果 self.process 为无:
AttributeError: 'Service' 对象没有属性 'process'
我该如何解决?
【问题讨论】:
FileNotFoundError: [WinError 2] The system cannot find the file specified
。看起来您提供的路径无效。看到这个问题:***.com/questions/29869757/…
【参考方案1】:
-
提供phantomjs.exe路径
driver = webdriver.PhantomJS('./phantomjs.exe')
将您的 selenium 版本升级到最新版本,然后尝试运行它
【讨论】:
【参考方案2】:问题是您的路径中缺少exe扩展程序。
试试这个:
from selenium import webdriver
phantomjs_path = r'C:\Phantom\phantomjs-2.0.0-windows\bin\phantomjs.exe'
browser = webdriver.PhantomJS(phantomjs_path)
【讨论】:
谢谢!但是我仍然收到异常:`Exception被忽略在:以上是关于无法在 Windows 7 中使用 Python 启动 PhantomJS的主要内容,如果未能解决你的问题,请参考以下文章
pip 无法在 Python 2.7、Windows 7 中安装模块
无法在 Windows 7 机器中使用 OpenCV 2.4.3、Python 2.7 打开“.mp4”视频文件
无法使用 Python 2.7、PyQt 4.11 和 Windows 7 64 位为 QFileDialog 设置默认过滤器
无法在 Windows 10 和 Python 3.7 上安装 dlib