已解决selenium.common.exceptions.WebDriverException: Messag: ‘geckodriver‘ executable needs to be in PA

已解决selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.




文章目录

  • 报错代码
  • 报错翻译
  • 报错原因
  • 解决方法




报错代码


我的代码(使用selenium自动化操作火狐浏览器 ):

from selenium import webdriver

# 初始化浏览器对象
driver = webdriver.Firefox()

# 发送请求
driver.get('https://bot.sannysoft.com/')

# 关闭浏览器
driver.quit()

报错信息:

Traceback (most recent call last):
  File "F:\Python3.8\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "F:\Python3.8\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "F:\Python3.8\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/Python学习/1.py", line 4, in <module>
    driver = webdriver.Firefox()
  File "F:\Python3.8\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 172, in __init__
    self.service.start()
  File "F:\Python3.8\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. 


报错翻译


报错内容翻译:常见的例外情况。WebDriverException:消息:“geckodriver”可执行文件需要位于路径中。



报错原因


报错原因:没有火狐浏览器驱动WebDriver,selenium没法操作浏览器,需要下载浏览器驱动,跟着下文一起下载



解决方法


Firefox浏览器驱动下载地址:https://github.com/mozilla/geckodriver/releases/


注意需要下对应浏览器版本的驱动,下载安装步骤如下:


  • 1. 确定浏览器的版本:

  • 2. 找到最相近的那个点击,下载对应的Chrome驱动版本

  • 3. 选择合适的操作系统

已解决selenium.common.exceptions.WebDriverException: Messag: ‘geckodriver‘ executable needs to be in PA_第1张图片


  • 4. 安装包解压后放进Python安装路径下
    已解决selenium.common.exceptions.WebDriverException: Messag: ‘geckodriver‘ executable needs to be in PA_第2张图片

  • 5. 再次启动程序就不会报错了

你可能感兴趣的:(《告别Bug》,selenium,python,firefox,自动化,测试工具)