Selenium in Python on Mac - Geckodriver executable needs to be in PATH
SOLVED: I placed the geckodriver exe in /Users/sethkillian/anaconda/bin and now it works from Spyder with no problem. Thanks for the help!
Download the geckodriver and put it in /usr/local/bin; then use webdriver.Firefox like this:
from selenium import webdriver
driver = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
simply download the executable that matches your os from here executables
unzip and put the executable in desired folder in your project
use **os ** or any path library to get the path your executable
import os
from selenium import webdriver
path_executable = os.path.abs( path/to/executable )
browser = webdriver.Firefox( executable_path= path_executable )
or you need something flexible which will work no matter the os and no need to download executable but its slower use pip to install webdriver manager webdriver manager doc$ pip install webdriver_manager
then from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
browser=webdriver.Firefox(executable_path=GeckoDriverManager().install())
Perhaps someone can explain why the path isn't found. And I also hope this helps someone else troubleshoot their own path issues.
You can certainly put the geckodriver executible anywhere you'd like. On my Mac, I chose ~/.local/bin since its a common place for executables to be stored that are specific to a user account. For example. the Heroku CLI is placed in ~/.local/share. This approach also eliminates the need for superuser access when adding an executable to a system location like /usr/local/bin
I then added it to the path within my .profile with
EXPORT PATH=$PATH:~/.local/bin
I tested by opening a terminal and checking with:
geckodriver --version
which worked fine.
But from a Python virtual environment, for some reason, the system path isn't passed?? I discovered this by adding to my selenium test script:
import sys
for p in sys.path:
print(p)
Which showed:
/Users/philip/Devel/myproject
/Users/philip/.virtualenvs/myproject/lib/python36.zip
/Users/philip/.virtualenvs/myproject/lib/python3.6
/Users/philip/.virtualenvs/myproject/lib/python3.6/lib-dynload
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
/Users/philip/.virtualenvs/myproject/lib/python3.6/site-packages
So ultimately I had to specify the path with:
self.browser = webdriver.Firefox(executable_path=r'/Users/philip/.local/bin/geckodriver')
This approach works fine, but I'd still like to know why I couldn't set the path in the virtual environment.