9
votes

I'm trying to run PhantomJS driver in Python but I'm getting error. I've read that I should pass the whole path as an argument but it didn't help.

Here is the code:

from selenium import webdriver

# driver = webdriver.Chrome('D:\Python_projects\chromedriver_win32/chromedriver.exe') # this works
driver = webdriver.PhantomJS(executable_path='D:\Python\phantomjs-2.0.0-windows\bin\phantomjs.exe')

ERROR:

Traceback (most recent call last):
  File "path to script", line 8, in <module>
    driver = webdriver.PhantomJS(executable_path='D:\Python\phantomjs-2.0.0-windows\bin\phantomjs.exe')
  File "C:\Python27\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py", line 50, in __init__
    self.service.start()
  File "C:\Python27\lib\site-packages\selenium\webdriver\phantomjs\service.py", line 75, in start
    raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
selenium.common.exceptions.WebDriverException: Message: Unable to start phantomjs with ghostdriver.
Screenshot: available via screen

Do you know what am I doing wrong?

4
@MalikBrahimi the documentation says that the path to the executable should be given - selenium.googlecode.com/svn/trunk/docs/api/py/… - fixxxer
Why not add the directory containing the executable in the system path? - Malik Brahimi
Can't repro with Selenium 2.44.0 and PhantomJS 2.0.0 on Windows. - Artjom B.

4 Answers

12
votes

Make the path in raw string, add 'r': 

driver = webdriver.PhantomJS(executable_path=r'D:\Python\phantomjs-2.0.0-windows\bin\phantomjs.exe')
10
votes

For simplicity's sake place the executable in the same directory as your script:

driver = webdriver.PhantomJS() # now there's no need for a path
0
votes

For me none of the above solved the problem; I found the code:

driver = webdriver.PhantomJS()

Will only work for root...

0
votes

For me, this works fine and also don't have to specify path for PhantomJS You need to install pip install phantomjs-binary binary package using pip

pip install phantomjs-binary

it will download a package of approx 60MB. and contains PhantomJS for windows, mac, and Linux as per the running environment. after that, you can use it

from selenium import webdriver
from phantomjs_bin import executable_path

driver = webdriver.PhantomJS(executable_path=executable_path)