11

This is embarrassing to ask because it seems like something with so slim chance of error. I wouldn't think this would be difficult, but I've been plugging away at this for almost 3 hours now and it's giving me a headache. I've read several dozen stackoverflow threads and Google threads.

I've installed PhantomJS, added it to my System Variables PATH, and it works properly in the command line. I also installed Selenium earlier with easy_install.

The error I get is:

__init__    C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\phantomjs\webdriver.py   50      

start   C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\phantomjs\service.py 66      

WebDriverException: Message: 'Unable to start phantomjs with ghostdriver.' ; Screenshot: available via screen               

Here's my code:

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path="C:\Python27\misc\phantomjs\phantomjs.exe")

I also tried:

from selenium import webdriver

driver = webdriver.PhantomJS()

I get the same error message. This has to be something simple that I'm doing wrong. I'd appreciate any comments or answers.

Windows 7 64 bit Python 2.7

User
  • 20,562
  • 35
  • 99
  • 185
  • Yes, I copied that path and pasted it, no mistakes. I also just ran it in command line and it changed to "phantomjs>". So all is well there. I'm thinking it may be a version mismatch or something? This is really stumping me. – User Feb 14 '14 at 01:30
  • 1.9.7. Path is: C:\Python27\misc\phantomjs\ – User Feb 14 '14 at 01:51
  • 1
    I can't reproduce it. Please try put into another path and try again. The error message says path is wrong. – Yi Zeng Feb 14 '14 at 02:27
  • Yeah, I already tried that before posting. So strange. When I said version mismatch, I was thinking between Selenium and PhantomJS but they're both freshly installed today. – User Feb 14 '14 at 02:31
  • It's getting stranger. I changed every / to // and then ran the unsaved python script in pyscripter and it gave me the error: IOError: [Errno 13] Permission denied: 'ghostdriver.log'. Which I think is progress. Then I saved it, and the error came back. This problem has something to do with references of the path, or how the path is being interpreted. – User Feb 15 '14 at 01:19
  • I tried running 'phantomjs --webdriver = 5000' and a bunch of random ports, but it says "Can't open '5000'" – User Feb 15 '14 at 17:34
  • I think it's a bug: http://code.google.com/p/selenium/issues/detail?id=6736 – User Feb 15 '14 at 17:38
  • hmm, Permission denied: 'ghostdriver.log' you say? from where were you executing your python script? most likely PhantomJS didn't have permissions to create a file in that directory, see my answer on how to prevent PhantomJS from making a ghostdriver.log file – Loknar Jul 04 '14 at 18:28

3 Answers3

13

This may have been a version issue for you, but since I just went through setting this up on my Windows 7 PC without issues I'm going to share my 'journey' here.

First of, I'm more used to the Mac/Linux Terminal and having the python package manager pip at my disposal is essential to me. After installing Python 2.7.8 and adding ;c:\Python27 to my PATH I noticed that pip isn't included with Python versions lower than 2.7.9, so I had to add it myself. Afterwards I added ;c:\Python27\Scripts to my PATH.

After that fetching the python package selenium was as easy as typing the following into the cmd:

pip install selenium

Then I downloaded the phantomjs-1.9.7-windows.zip from here, unzipped it and placed it here:

C:\Python27\misc\phantomjs-1.9.7-windows\phantomjs.exe

From there I had a working Python 2.7/Selenium Webdriver/PhantomJS example for Windows 7.

from selenium import webdriver
import os

phantomjs_path = "C:\Python27\misc\phantomjs-1.9.7-windows\phantomjs.exe"

browser = webdriver.PhantomJS(executable_path=phantomjs_path, service_log_path=os.path.devnull)
browser.set_window_size(1400, 1000)

browser.get("https://stackoverflow.com/")

print browser.title

Note that I added the argument service_log_path=os.path.devnull to the function webdriver.PhantomJS() to prevent PhantomJS from creating a ghostdriver.log in the directory of the python file being executed.

Community
  • 1
  • 1
Loknar
  • 1,084
  • 2
  • 12
  • 37
1

I had the same problem running Python 3.4 on Windows Server 2012 R2. PhantomJS was failing to create the ghostdriver.log file. I followed these steps that fixed it for me:

  • Made sure phantomjs.exe was not showing "Blocked" on the File Properties|Security tab, and ran it as standalone app to confirm.
  • Deleted an old copy of the ghostdriver.log file that was in the same directory.
  • Ran python REPL from the console while checking to see if the code that instantiated the driver was getting called successfully.

    browser = webdriver.PhantomJS(executable_path='phantomjs.exe', desired_capabilities=argdc, service_args=svc_args)
    
Cahit
  • 2,136
  • 14
  • 20
0

Do you any other file or directory with a same name , or a file of coding (like .. phantomjs.py) which you have named same as phantomjs is so then rename it to something else. i hope it works

P.hunter
  • 1,203
  • 2
  • 15
  • 39