我第一次尝试设置 Selenium。执行以下代码时:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
chrome_driver_path = ChromeDriverManager().install()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option(name="detach", value=True)
service = ChromeService(executable_path=chrome_driver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get(url="https://www.google.com")
我收到以下错误:
Traceback (most recent call last):
File "D:\Python\Day48a\main.py", line 13, in <module>
driver = webdriver.Chrome(service=service, options=chrome_options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
super().__init__(
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 66, in __init__
super().__init__(command_executor=executor, options=options)
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 241, in __init__
self.start_session(capabilities)
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 329, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 384, in execute
self.error_handler.check_response(response)
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from unknown error: cannot find Chrome binary
Stacktrace:
GetHandleVerifier [0x00E53433+25059]
(No symbol) [0x00DDCE34]
(No symbol) [0x00CBBEC3]
(No symbol) [0x00CE9570]
(No symbol) [0x00CE7169]
(No symbol) [0x00D2B4D6]
(No symbol) [0x00D2AC9A]
(No symbol) [0x00D21C96]
(No symbol) [0x00CF3FAC]
(No symbol) [0x00CF4F3D]
GetHandleVerifier [0x01145593+3113795]
GetHandleVerifier [0x0115A25A+3198986]
GetHandleVerifier [0x01152A32+3168226]
GetHandleVerifier [0x00EF32A0+680016]
(No symbol) [0x00DE577D]
(No symbol) [0x00DE2A28]
(No symbol) [0x00DE2BC5]
(No symbol) [0x00DD5820]
BaseThreadInitThunk [0x7510FCC9+25]
RtlGetAppContainerNamedObjectPath [0x770B809E+286]
RtlGetAppContainerNamedObjectPath [0x770B806E+238]
我尝试使用以下代码指定 Chrome 二进制路径(chromedriver.exe 位于同一位置):
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
chrome_driver_path = ChromeDriverManager().install()
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = r"C:\Program Files\Google\Chrome\Application"
chrome_options.add_experimental_option(name="detach", value=True)
service = ChromeService(executable_path=chrome_driver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get(url="https://www.google.com")
...只是得到以下错误:
Traceback (most recent call last):
File "D:\Python\Day48a\main.py", line 12, in <module>
driver = webdriver.Chrome(service=service, options=chrome_options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
super().__init__(
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 66, in __init__
super().__init__(command_executor=executor, options=options)
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 241, in __init__
self.start_session(capabilities)
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 329, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 384, in execute
self.error_handler.check_response(response)
File "D:\Python\Day48a\.venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Failed to create Chrome process.
Stacktrace:
GetHandleVerifier [0x00E53433+25059]
(No symbol) [0x00DDCE34]
(No symbol) [0x00CBBEC3]
(No symbol) [0x00CEA97B]
(No symbol) [0x00CE7169]
(No symbol) [0x00D2B4D6]
(No symbol) [0x00D2AC9A]
(No symbol) [0x00D21C96]
(No symbol) [0x00CF3FAC]
(No symbol) [0x00CF4F3D]
GetHandleVerifier [0x01145593+3113795]
GetHandleVerifier [0x0115A25A+3198986]
GetHandleVerifier [0x01152A32+3168226]
GetHandleVerifier [0x00EF32A0+680016]
(No symbol) [0x00DE577D]
(No symbol) [0x00DE2A28]
(No symbol) [0x00DE2BC5]
(No symbol) [0x00DD5820]
BaseThreadInitThunk [0x7510FCC9+25]
RtlGetAppContainerNamedObjectPath [0x770B809E+286]
RtlGetAppContainerNamedObjectPath [0x770B806E+238]
我的Chrome版本:131.0.6778.85
我该如何解决这个问题?
作为最后一次报告,我从我的电脑上完全卸载了 Chrome,然后重新安装了它,现在它似乎可以正常工作了。可能是 Chrome 浏览器的路径存在问题,但奇怪的是,对路径进行硬编码并没有解决它......