我在 python 的 visual studio 代码中设置 selenium 时遇到问题

I facing a problem in set up selenium in virtual studio code for python

from selenium import webdriver
chromedriver_location = "/Users/Sony/Desktop/XUAN/Python/chromedriver_win32/chromedriver"
driver = webdriver.Chrome()
driver.get('https://sapsnkra.moe.gov.my/ibubapa2/')
input(" ")

PS C:\Users\Sony\Desktop\XUAN\Python> python -u "c:\Users\Sony\Desktop\XUAN\Python\Python Project\tempCodeRunnerFile.py" 追溯(最近一次通话): 文件“C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\common\service.py”,第 71 行,开始 self.process = subprocess.Popen(cmd, 环境=self.env, 文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\subprocess.py”,第 951 行,在 init self._execute_child(args, 可执行文件, preexec_fn, close_fds, 文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\subprocess.py”,第 1420 行,在 _execute_child hp、ht、pid、tid = _winapi.CreateProcess(可执行文件、参数、 FileNotFoundError: [WinError 2] 系统找不到指定的文件

在处理上述异常的过程中,又发生了异常:

回溯(最后一次调用): 文件“c:\Users\Sony\Desktop\XUAN\Python\Python Project\tempCodeRunnerFile.py”,第 3 行,位于 driver = webdriver.Chrome() 文件“C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\chrome\webdriver.py”,第 70 行,在 init 中 super(WebDriver, self).init(DesiredCapabilities.CHROME['browserName'], "goog", 文件“C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\chromium\webdriver.py”,第 90 行,在 init 中 self.service.start() 文件“C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\common\service.py”,第 81 行,开始 引发 WebDriverException( selenium.common.exceptions.WebDriverException:消息:'chromedriver' 可执行文件需要在 PATH 中。请参阅 https://chromedriver.chromium.org/home

您实际上可以使用 webdriver-manager 来自动管理您的 chromedriver 版本。

你可以这样使用它:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://sapsnkra.moe.gov.my/ibubapa2/')
input(" ")