selenium打开浏览器报错:selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ execu

运行如下代码:

# encoding: utf-8 -*-
# 测试Chromedriver的环境是否配置好(path)
from selenium import webdriver
import time


def main():
    b = webdriver.Chrome()
    b.get('http://www.baidu.com')
    time.sleep(5)
    b.quit()


if __name__ == '__main__':
    main()

错误信息如下:

Traceback (most recent call last):
File “D:\Tool\Python36\lib\site-packages\selenium\webdriver\common\service.py”, line 76, in start
stdin=PIPE)
File “D:\Tool\Python36\lib\subprocess.py”, line 709, in init
restore_signals, start_new_session)
File “D:\Tool\Python36\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 “D:/Tool/PycharmProjects/chapter11/seleniumExample.py”, line 2, in
browser = webdriver.Chrome()
File “D:\Tool\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py”, line 68, in init
self.service.start()
File “D:\Tool\Python36\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: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Process finished with exit code 1
解决办法:
1、下载谷歌驱动
首先查看谷歌浏览器的版本
在浏览器输入:chrome://settings/help查看版本

webdriver下载地址:
http://npm.taobao.org/mirrors/chromedriver/
找到与浏览器最接近的版本下载


下载后直接解压即可
然后找到浏览器的路径,进入

然后将驱动拷贝进去

再把Chrome配置到环境变量中(path)

此时还未完成,接下来还需要把webdriver配置到Python中
查看python的路径:where is python


此时,大功告成

来源:十七光年

物联沃分享整理
物联沃-IOTWORD物联网 » selenium打开浏览器报错:selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ execu

发表评论