admin管理员组

文章数量:1538620

使用selenium,但chrome无法启动,需要安装浏览器对应版本(最新)的chromedriver


一、问题描述

  • 使用selenium无法启动chrome,代码
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://www.baidu')
driver.close()
  • 报错如下:
D:\python\python.exe D:\pycode\mypycode\case\test06_login.py 
Traceback (most recent call last):
  File "D:\python\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    creationflags=self.creationflags)
  File "D:\python\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "D:\python\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\pycode\mypycode\case\test06_login.py", line 9, in <module>
    driver = webdriver.Chrome()
  File "D:\python\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    service_log_path, service, keep_alive)
  File "D:\python\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 90, in __init__
    self.service.start()
  File "D:\python\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://chromedriver.chromium.org/home

二、安装最新版本

  • 安装地址

    • 查询了浏览器版本:117.0.5927

    • 但是国内的镜像网找不到,只更新到114版本

    • 需要安装驱动,点击了最下面的网址https://chromedriver.chromium/home

    • 如图为有效信息 - 点击跳转:https://googlechromelabs.github.io/chrome-for-testing(点击这里直接找到114以后的版本)

    • 找到对应版本,点击下载即可

    • 解压到python.exe目录下

  • 复制chromedriver.exe到chrome的安装目录下

  • 配置环境变量

  • 配置用户变量

  • 重启pycharm

三.测试成功

  • 跑一下
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get('http://www.baidu')
sleep(3)
driver.close()
  • 成功

本文标签: 无法启动浏览器版本最新selenium