Can't launch PhantomJS with Python in Windows

2019-04-13 15:36发布

问题:

I'm trying to use PhantomJS with Python in my Windows 7, but is not working! Here what i tried to do.

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/')

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

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( )

Error message:

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')

Error message:

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'

How can i fix it?

回答1:

The problem is that the exe extension is missing in your path.

Try this:

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


回答2:

  1. Provide phantomjs.exe path driver = webdriver.PhantomJS('./phantomjs.exe')
  2. Upgrade your selenium Version to latest and then try running it