问题
Mac OS user here. I'm trying to run a command in my python IDLE:
from selenium import webdriver
browser = webdriver.Firefox()
and I get the following error message:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
browser = webdriver.Firefox()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 160, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
I've ran brew install geckodriver
, and which geckodriver
returns /usr/local/bin/geckodriver
so I'm sure it's installed correctly. It seems like it's still not running properly however?
回答1:
Your error message is quite clear:
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
You have to set the path to the geckodriver. There are different ways to do that.
You could set the path to the geckodriver in the code:
from selenium import webdriver
browser = webdriver.Firefox(executable_path=r'/.../path2Your/geckodriver')
or insert the path to the geckodriver in the PATH environment variable:
export PATH=$PATH:/.../path2Your/geckodriver
I never tried to move the executable in the /usr/local/bin/
in a mac os. I tried in an ubuntu os and it works. I think should be fine.
Probably it's because the file is not executable. If it isn't, go to /usr/local/bin/
and make it executable:
chmod +x geckodriver
回答2:
One of the things that best worked for me is just copy the executable geckodriver file.
Go into the bin folder:
If your on linux you can find it in the below location:
/home/user_name/.local/bin
Paste your geckodriver exe here.
So your final code should be like this:
from selenium import webdriver
driver = webdriver.Firefox(executable_path = 'geckodriver')
This worked for me like charm but am not sure whether it is the right way.
回答3:
I'm using ubuntu 18.04 and had the same issue here.
In my case, this works fine. Hope that works for you!
sudo apt-get install firefox-geckodriver
来源:https://stackoverflow.com/questions/50461779/geckodriver-executable-needs-to-be-in-path