Python+Selenium 打开火狐浏览器报错问题:FileNotFoundError: [WinError 2] 系统找不到指定的文件

搭建好Python3.6+Firefox+Selenium环境,运行如下测试脚本:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.baidu.com')

得到如下报错:

C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Administrator/PycharmProjects/WebAuto/IEAuto.py
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/WebAuto/IEAuto.py", line 4, in 
    browser = webdriver.Firefox()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
    self.service.start()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\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. 
Process finished with exit code 1

这个报错包含两个问题:

  1. subprocess.py文件里的shell模式没有开启
    解决方式:找到并编辑文件C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py
    把函数def __init__里的shell=False改为shell=True
    配置完此步骤再执行脚本会得到如下报错:
    C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Administrator/PycharmProjects/WebAuto/IEAuto.py
    Traceback (most recent call last):
    File “C:/Users/Administrator/PycharmProjects/WebAuto/IEAuto.py”, line 4, in
    browser = webdriver.Firefox()
    File “C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py”, line 164, in init
    self.service.start()
    File “C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py”, line 98, in start
    self.assert_process_still_running()
    File “C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py”, line 111, in assert_process_still_running
    % (self.path, return_code)
    selenium.common.exceptions.WebDriverException: Message: Service geckodriver unexpectedly exited. Status code was: 1

  2. 没有添加浏览器驱动
    解决方式:

  1. 到网址https://github.com/mozilla/geckodriver/releases,根据自己的电脑,下载的win64位的;
  2. 把文件解压到python安装目录
    3)在环境变量PATH里面把驱动所在目录的路径添加进去

此时执行python脚本
问题完美解决。

你可能感兴趣的:(Python+Selenium 打开火狐浏览器报错问题:FileNotFoundError: [WinError 2] 系统找不到指定的文件)