【问题标题】:Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home消息:“chromedriver”可执行文件需要在 PATH 中。请参阅 https://sites.google.com/a/chromium.org/chromedriver/home
【发布时间】:2021-09-10 16:20:51
【问题描述】:

我之前已经多次看到这个问题提出了很好的解决方案。

事实上,我可以在从编辑器 (VS code) 运行代码时让它工作。 但是,当我创建一个批处理文件 *.bat 并尝试运行相同的文件时,我收到以下错误消息:

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

调用代码的批处理文件如下所示:

"C:\Program Files\Python39\python.exe" "Y:\path\to\python_file.py"

exe和文件在不同的文件夹C:Y:

我在这里使用selenium 的常用代码:

from selenium import webdriver
PATH = r'Y:\some_path_to\chromedriver.exe'
driver = webdriver.Chrome(PATH)

我尝试过: https://sites.google.com/chromium.org/driver/getting-started

我也试过这个: Error message: "'chromedriver' executable needs to be available in the path"

我也尝试过更改 os.path: https://www.geeksforgeeks.org/python-os-chdir-method/

我进一步尝试了不同的方法和变体来让代码从批处理文件运行(同样,相同的代码从VS code 编辑器运行),但不知道如何解决错误?

额外说明: 我也尝试过改变windows环境下的路径: https://zwbetz.com/download-chromedriver-binary-and-add-to-your-path-for-automated-functional-testing/

但这不起作用。

【问题讨论】:

  • 我也遇到了类似的问题,给个链接
  • 这能回答你的问题吗? stackoverflow.com/questions/67901942/…
  • 我已经尝试了不同的变体,但不起作用。有什么具体的建议吗?

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

这可能是您的虚拟环境有问题吗?当您从 VS Code 运行脚本时,默认情况下它使用选定的 venv。从 .bat 文件中它没有。 尝试在不激活虚拟环境的情况下从 PowerShell(或 cmd)手动运行脚本,看看是否遇到相同的错误。

【讨论】:

  • 感谢您的评论。我不使用虚拟环境,所以很遗憾不能这样。
  • 记住:永远不要将答案用作 cmets。
【解决方案2】:

我发现这段代码对我有用

python 脚本代码

import os
import selenium
from selenium import webdriver as wb

webd = wb.Chrome(executable_path=r'D:\Projects\Tools\Chrome Driver\chromedriver.exe')
webd.get('https://selenium-python.readthedocs.io/getting-started.html')

批处理文件代码

@echo off
"D:\Projects\Tools\environment\Scripts\python.exe" "D:\Projects\Python Scripts\chrome_driver.py"
pause

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2017-12-08
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多