首页 > 解决方案 > 错误“chromedriver”可执行文件需要在 PATH 中

问题描述

selenium.common.exceptions.WebDriverException: Message: '' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home当我尝试运行此代码时会出现此问题。我已经从链接下载了文件并将其解压缩到我的下载文件夹,如下所示: C:\Users\Alexandr\Downloads\chromedriver_win32 。然后我将可执行二进制文件 (C:\Users\michael\Downloads\chromedriver_win32) 的路径放入环境变量“Path”中。

我在 StackOverflow 上找到了相同的线程chromedriver,他们说我应该输入cmd 并得到类似的东西tarting ChromeDriver 2.15.322448,就我而言,我得到"chromedriver" is not inner or outer command了(这是我的母语的翻译)。在这种情况下我该怎么办

from selenium import webdriver
from time import sleep

class InstaBot:

    def __init__(self, username, password):
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.instagram.com/?hl=ru')
        sleep(2)
        self.driver.find_element_by_xpath("//input[@name=\"username\"]") \
            .send_keys(username)
        self.driver.find_element_by_xpath("//input[@name=\"password\"]") \
            .send_keys(password)
        self.driver.find_element_by_xpath('//button[@type="submit"]') \
            .click()
        sleep(4)
        self.driver.find_element_by_xpath('//button[contains(text()."Не сейчас")]').click()


InstaBot('_max_leva_', here is my password)

这是错误代码:

C:\Users\Alexandr\AppData\Local\Programs\Python\Python38-32\python.exe C:/PythonProjects/python/LearningPython/main1.py
Traceback (most recent call last):
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38-32\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 "C:/PythonProjects/python/LearningPython/main1.py", line 27, in <module>
    InstaBot('_max_leva_', '447781470659')
  File "C:/PythonProjects/python/LearningPython/main1.py", line 14, in __init__
    self.driver = webdriver.Chrome()
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
    raise WebDriverException(
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

环境变量:

%PATH%:C:\Users\Alexandr\Downloads\chromedriver_win32

标签: pythonseleniumselenium-webdriver

解决方案


在 中self.driver = webdriver.Chrome(),将可执行文件的路径作为括号中的参数传递。

例如:

self.driver = webdriver.Chrome('C:/user/Downloads/chromedriver.exe')


推荐阅读