gpt4 book ai didi

python - 无法在 Windows 7 中使用 Python 启动 PhantomJS

转载 作者:可可西里 更新时间:2023-11-01 09:47:32 24 4
gpt4 key购买 nike

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

错误信息:

Exception ignored in: > Traceback (most recent call last): File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 136, in del File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 124, in stop AttributeError: 'NoneType' object has no attribute 'close'

在这里,如果我稍微更改一下代码:

from selenium import webdriver
browser = webdriver.PhantomJS(executable_path='C:\Phantom\phantomjs-2.0.0-windows\bin\phantomjs.exe')

错误信息:

Traceback (most recent call last):

File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 62, in start

stdout=self.log_file, stderr=self.log_file)

File "c:Python34\lib\subprocess.py", line 859, in init

restore_signals, start_new_session)

File "c:Python34\lib\subprocess.py", line 1112, in _execute_child startupinfo)

FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "d:\test.py", line 2, in

browser = webdriver.PhantomJS(executable_path='C:\Phantom\phantomjs-2.0.0-windows\bin\phantomjs')

File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 51, in init self.service.start( )

File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 69, in start os.path.basename(self.path),

self.start_error_message)

selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH.

Exception ignored in: del of

>

Traceback (most recent call last):

File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 136, in del self.stop( )

File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 117, in stop

if self.process is None:

AttributeError: 'Service' object has no attribute 'process'

我该如何解决?

最佳答案

问题是您的路径中缺少exe 扩展名

试试这个:

from selenium import webdriver
phantomjs_path = r'C:\Phantom\phantomjs-2.0.0-windows\bin\phantomjs.exe'
browser = webdriver.PhantomJS(phantomjs_path)

关于python - 无法在 Windows 7 中使用 Python 启动 PhantomJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34840109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com