简体   繁体   中英

problem with browser = webdriver.Firefox()

My code (from eg here https://pypi.org/project/selenium/ )

from selenium import webdriver

browser = webdriver.Firefox()

Error Traceback (most recent call last): File "C:\Users\saltlake\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start self.process = subprocess.Popen(cmd, env=self.env, File "C:\Users\saltlake\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 854, in init self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\saltlake\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1307, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "D:\Project\python-work\website.py", line 3, in browser = webdriver.Firefox() File "C:\Users\saltlake\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in init self.service.start() File "C:\Users\saltlake\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

Anybody aware this problem/fix?

You're getting this error because you don't have selenium web driver for firefox.

To get it, go to the website https://github.com/mozilla/geckodriver/releases

Download the version suitable for your os.

Extract it

Place the extracted file in the same directory you're running this script.

Ie your "website.py" file must be in the same directory as the extracted file

Viola!!!

The error here is for the unavailability of a firefox web driver to work with.

There are 2 ways you can have the driver,

  1. Go to this website Firefox web driver for python , and download the latest version of the web driver, but that'll help you run the code on your locale only, everytime you run code on another machine it'll ask for rewriting the path you'll give for web driver. If you haven't included the.exe file in the project folder.

  2. The better approach is to get familiarize with the webdriver manager python , which makes your code more portable and robust, which helps you set up the driver you need automatically. This approach will help you write efficient codes at the beginning itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM