Daniel Chepenko
Daniel Chepenko

Reputation: 2268

PhantomJS with Selenium: Message: 'phantomjs' executable needs to be in PATH

I having trouble installing PhantomJS to my project Following the suggestions from the similar question from so I defined the $PATH variable with executable path

cd Users/zkid18/project/venv/venv_name/lib/python3.6/site-packages/phantomjs-2.1.1/bin 
export PATH=$PWD

Then I try to create a driver with a virtual browser

import from selenium import webdriver
browser = webdriver.PhantomJS()

On this step I got an error

No such file or directory: 'phantomjs': 'phantomjs'

What I am missing?

Upvotes: 2

Views: 3583

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

This error message...

No such file or directory: 'phantomjs': 'phantomjs'

...implies that the program was unable to locate the phantomjs binary.

As you are on MAC OS X you need to download phantomjs-2.1.1-macosx.zip from Download PhantomJS page and extract (unzip) the content within your system. Next you can mention the absolute path of phantomjs binary passing the argument executable_path as follows:

  • MAC OS X example:

    from selenium import webdriver
    
    driver = webdriver.PhantomJS(executable_path='/path/to/phantomjs-2.1.1-xxx/bin/phantomjs')
    driver.get('https://www.google.com/')
    print(driver.title)
    driver.quit()
    
  • Windows OS example:

    from selenium import webdriver
    
    driver = webdriver.PhantomJS(executable_path=r'C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
    driver.get('https://www.google.com/')
    print(driver.title)
    driver.quit()
    

Upvotes: 1

Moshe Slavin
Moshe Slavin

Reputation: 5204

You need to give the path:

browser = webdriver.PhantomJS(executable_path='Complete path/to/phantomjs')

To find it use export PATH=${PATH:+$PATH:} in the command line as @Anderson commented.

Upvotes: 2

Related Questions