Selenium Python Headless Webdriver (PhantomJS) Not Working
Asked Answered
N

2

7

So I'm having trouble getting selenium to work with a headless driver, specifically PhantomJS. I'm trying to get it to work on an Ubuntu webserver (Ubuntu 14.04.2 LTS).

Running the following commands from a python interpreter (Python 2.7.6) gives:

from selenium import webdriver
driver = webdriver.PhantomJS()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/webdriver.py", line 51, in __init__
    self.service.start()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/service.py", line 76, in start
    raise WebDriverException("Unable to start phantomjs with ghostdriver: %s" % e)
selenium.common.exceptions.WebDriverException: Message: Unable to start phantomjs with ghostdriver: [Errno 2] No such file or directory

I've also tried:

driver = webdriver.PhantomJS(executable_path="/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/webdriver.py", line 51, in __init__
    self.service.start()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/service.py", line 76, in start
    raise WebDriverException("Unable to start phantomjs with ghostdriver: %s" % e)
selenium.common.exceptions.WebDriverException: Message: Unable to start phantomjs with ghostdriver: [Errno 13] Permission denied

I also added it to the python path:

import sys
sys.path.append("/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/")

I am currently logged in as root. Permissions for the phantomjs directory are:

drwxr-sr-x  2 root staff 4096 Sep  9 06:58 phantomjs

and for phantomjs/webdriver.py:

-rw-r--r--  1 root root  2985 Sep  9 06:58 webdriver.py

I've confirmed selenium is installed and up-to-date (pip install selenium --upgrade). It is installed at:

/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/

print selenium.__version__
2.47.1

I've looked at:

I've been testing my program on a locally hosted server (on OSX), using chromedriver. I'm actually using Splinter (https://splinter.readthedocs.org/en/latest/#headless-drivers) for that, and have tried the other headless drivers (django and zope.testbrowser) but have had similar issues.

I'm open to any suggestions, I don't mind changing driver if required.

Thanks in advance for any help.

Northey answered 10/9, 2015 at 0:42 Comment(0)
K
12

I had the same problem as you with the same errors. I have tried to install it on openSuse Server. I ended up installing PhantomJS form source -unfortunately without any success. The way that worked for me was installing Phantomjs via npm

sudo npm install -g phantomjs
Kenric answered 9/10, 2015 at 11:38 Comment(1)
Solved my question using Linux MintMineraloid
N
4

So installing phantomjs through the package manager seemed to fix it...

sudo apt-get install phantomjs

Anyway hopefully the process above helps other people who have the same issue.

Northey answered 20/9, 2015 at 0:52 Comment(1)
For people who have the issue on OSX Yosemite, I was getting the following error: selenium.common.exceptions.WebDriverException: Message: Can not connect to GhostDriver on port XXXX Which was linked to the error Killed: 9 when running phantomjs from terminal. These links fixed it for me: #28268309 github.com/eugene1g/phantomjs/releasesNorthey

© 2022 - 2024 — McMap. All rights reserved.