from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service # 20240627添加
# 无头浏览器
chrome_options = Options()
chrome_options.add_argument('--headless')
chromedriver_path = r"G:\\python_work\\chrome\\chromedriver.exe"
chrome_service = Service(executable_path=chromedriver_path)
driver = webdriver.Chrome(options=chrome_options, service=chrome_service)#**这一行开始报错**
url = 'http://www.baidu.com'
driver.get(url)
driver.close()
Traceback (most recent call last):
File "G:\python_work\ccccc.py", line 12, in <module>
driver = webdriver.Chrome(options=chrome_options, service=chrome_service)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
super().__init__(
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 66, in __init__
super().__init__(command_executor=executor, options=options)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 212, in __init__
self.start_session(capabilities)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 299, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 354, in execute
self.error_handler.check_response(response)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
**** raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary****
Stacktrace:
Backtrace:
(No symbol) [0x01426643]
(No symbol) [0x013BBE21]
(No symbol) [0x012BDA9D]
(No symbol) [0x012DCC30]
(No symbol) [0x012DA899]
(No symbol) [0x01316917]
(No symbol) [0x0131655C]
(No symbol) [0x0130FB76]
(No symbol) [0x012E49C1]
(No symbol) [0x012E5E5D]
GetHandleVerifier [0x0169A142+2497106]
GetHandleVerifier [0x016C85D3+2686691]
GetHandleVerifier [0x016CBB9C+2700460]
GetHandleVerifier [0x014D3B10+635936]
(No symbol) [0x013C4A1F]
(No symbol) [0x013CA418]
(No symbol) [0x013CA505]
(No symbol) [0x013D508B]
BaseThreadInitThunk [0x7638344D+18]
RtlInitializeExceptionChain [0x77879802+99]
RtlInitializeExceptionChain [0x778797D5+54]
win7的chrome版本没有更新,版本和chromedriver.exe都一致,版本号109。之前一直是能用selenium(4.6)打开chrome的(chromedriver.exe的目录没有添加到环境变量,),今天几次(甚至selenium升级到4.22后)也能打开,但后面有一次就突然出错了,然后就一直报错。代码在另外一台电脑上也能打开,正常运行。是不是本机浏览器内存溢出?重启电脑多次也没用