python - 无头 Python Selenium 显示错误“chromedriver”可执行文件需要在 PATH 中
问题描述
我正在运行一个 selenium 脚本,我想在不使用 chrome 选项打开浏览器的情况下运行我的代码。我尝试将驱动程序设置为 PATH,然后将其更改为 options=op 但它显示了相同的错误(我的代码中的文件路径是准确的)。我按照这篇文章的第二个答案并尝试在 cmd 中运行 chromedriver.exe(不确定这是否相关,但我在另一篇文章中看到了这个并认为它可能有用)。下面的片段是输出:
c:\Program Files (x86)>chromedriver
Starting ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
这是我的代码:
from selenium import webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
op = webdriver.ChromeOptions()
op.add_argument('headless')
# driver = webdriver.Chrome(PATH)
driver = webdriver.Chrome(options=op)
这是我收到的错误:
Traceback (most recent call last):
File "C:\Users\soodr\PythonFiles\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\soodr\PythonFiles\Lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\soodr\PythonFiles\Lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/soodr/PycharmProjects - Copy/pythonProject1/linkedenInfo.py", line 16, in <module>
driver = webdriver.Chrome(options=op)
File "C:\Users\soodr\PythonFiles\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\soodr\PythonFiles\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
我定义驱动程序的方式有问题吗?
解决方案
你的字符串是错误的。如果您想\
在字符串中使用,请执行原始字符串PATH = r"C:\Program Files (x86)\chromedriver.exe"
推荐阅读
- .net-core - Microsoft.AspNetCore.SignalR.Client - 启用响应压缩
- python - Python Openpyxl - Merge_Cells()
- java - SearchView 到 Edittext 搜索
- linux - 如何从命令行确定我的 KDE 桌面是否处于锁屏状态?
- java - 找不到适合 jdbc:postgresql 的驱动程序:
- javascript - 如何将 Dialogflow Fulfillment 与 MySQL DB 连接?
- php - 意外的php usort结果
- java - 在java中将对象从一个类转换为另一个类
- php - 禁用某些端点的数据库会话
- oauth-2.0 - 使用授权码流程刷新令牌流程