无法在 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被忽略在:>的>> Traceback(最近一次调用):>File“c:Python34 \lib\site-packages\selenium\webdriver\common\service.py",第 136 行,在 del >文件 "c:Python34\lib\site-packages\selenium\webdriver\common\service .py",第 124 行,在停止 AttributeError 中:'NoneType' 对象没有属性 'close' 这是另一个例外 :) 你是如何关闭浏览器的?你能把你的完整代码放在github上让我看看吗?您似乎没有使用 browser.close()browser.quit() 来结束会话 @Amanda 如果这个答案回答了你的问题,你能接受吗?如果我能进一步帮助你,我会很乐意这样做:) 抱歉这么晚才回复。我没有看到你的答案。我的整个代码就是我发布的。只是一个测试代码。我尝试包含 browser.close() 和 browser.quit()。最后一个我收到了这条消息: 试试看this question

以上是关于无法在 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

为 Windows 激活 venv (Python 3.7.2) [重复]

无法在 Windows 中的 python 2.7.9 上安装 scikit-earn?