无法在Windows 7中使用Python启动PhantomJS [英] Can't launch PhantomJS with Python in Windows 7
问题描述
我正在Windows 7中尝试将PhantomJS与Python结合使用,但无法正常工作! 这是我试图做的.
I'm trying to use PhantomJS with Python in my Windows 7, but is not working! Here what i tried to do.
首先,我安装了webdriver.在以下代码中,Firefox可以正常打开,因此我相信已正确安装了webdrive.
First, i installed webdriver. In the following code Firefox opens normally, so i believe webdrive is correctly installed.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
然后,我从官方网站下载了PhantomJS,将其解压缩并放在c:\ Phantomjs中.然后我将其添加到环境变量中. 这是里面的东西:
Then, i downloaded PhantomJS from the official website, unpacked it and put it in c:\Phantomjs. Then i added it in Environment Variables. Here what is in there:
C:\ Phantom \ phantomjs-2.0.0-windows \ bin
C:\Phantom\phantomjs-2.0.0-windows\bin
因此,当我在终端中键入"phantomjs"时,它将正常启动.但是以下代码给了我一个错误:
So, when i type "phantomjs" in my terminal, it starts normally. But the following code gives me an error:
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"
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'
在这里,如果我更改一点代码:
And here if i change a little bit the code:
from selenium import webdriver
browser = webdriver.PhantomJS(executable_path='C:\Phantom\phantomjs-2.0.0-windows\bin\phantomjs.exe')
错误消息:
回溯(最近通话最近一次):
Traceback (most recent call last):
文件"c:Python34 \ lib \ site-packages \ selenium \ webdriver \ common \ service.py",在开始处的第62行
File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 62, in start
stdout = self.log_file,stderr = self.log_file)
stdout=self.log_file, stderr=self.log_file)
"strong:init
File "c:Python34\lib\subprocess.py", line 859, in init
restore_signals,start_new_session)
restore_signals, start_new_session)
_execute_child startupinfo中的文件"c:Python34 \ lib \ subprocess.py",第1112行)
File "c:Python34\lib\subprocess.py", line 1112, in _execute_child startupinfo)
FileNotFoundError:[WinError 2]系统找不到指定的文件
FileNotFoundError: [WinError 2] The system cannot find the file specified
在处理上述异常期间,发生了另一个异常:
During handling of the above exception, another exception occurred:
回溯(最近通话最近一次):
Traceback (most recent call last):
浏览器= webdriver.PhantomJS(executable_path ='C:\ Phantom \ phantomjs-2.0.0-windows \ bin \ phantomjs')
browser = webdriver.PhantomJS(executable_path='C:\Phantom\phantomjs-2.0.0-windows\bin\phantomjs')
init self.service.start()
File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 51, in init self.service.start( )
在开始os.path.basename(self.path)中的文件"c:Python34 \ lib \ site-packages \ selenium \ webdriver \ common \ service.py"的第69行,
File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 69, in start os.path.basename(self.path),
self.start_error_message)
self.start_error_message)
selenium.common.exceptions.WebDriverException:消息:"phantomjs"可执行文件必须位于PATH中.
selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH.
在
>
回溯(最近通话最近一次):
Traceback (most recent call last):
del self.stop()
File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 136, in del self.stop( )
文件"c:Python34 \ lib \ site-packages \ selenium \ webdriver \ common \ service.py",第117行,处于停止状态
File "c:Python34\lib\site-packages\selenium\webdriver\common\service.py", line 117, in stop
如果self.process为None:
if self.process is None:
AttributeError:服务"对象没有属性进程"
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)
这篇关于无法在Windows 7中使用Python启动PhantomJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!