问题
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?
回答1:
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.
回答2:
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()
来源:https://stackoverflow.com/questions/54133200/phantomjs-with-selenium-message-phantomjs-executable-needs-to-be-in-path