在 Chrome WebDriver 中使用 Selenium 时遇到“已断开连接:无法从渲染器接收消息”错误。
连接到 VPN 并尝试使用 WebDriver 打开网页后会出现此问题。
我需要在连接 VPN 之前启动 WebDriver。
在 VPN 连接之前获取网页效果良好。
我尝试了各种解决方案,包括调整 ChromeOptions,但错误仍然存在。
有人可以提供解决此问题的见解或解决方案吗?谢谢你。
import subprocess
import time
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(
options=options
)
subprocess.Popen(
'rasdial "jp-free-09.protonvpn.net"',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True
)
print('Connected to VPN.')
time.sleep(10)
driver.get('https://stackoverflow.com/')
driver.quit()
print('Completed.')
Traceback (most recent call last):
File "c:\Users\****\****\****\****\****.py", line 23, in <module>
driver.get('https://stackoverflow.com/')
File "C:\Users\****\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 355, in get
self.execute(Command.GET, {"url": url})
File "C:\Users\****\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 346, in execute
self.error_handler.check_response(response)
File "C:\Users\****\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: disconnected: not connected to DevTools
(failed to check if window was closed: disconnected: not connected to DevTools)
(Session info: chrome=115.0.5790.102)
Stacktrace:
Backtrace:
GetHandleVerifier [0x00A5A813+48355]
(No symbol) [0x009EC4B1]
(No symbol) [0x008F5358]
(No symbol) [0x008E7D96]
(No symbol) [0x008E7AB9]
(No symbol) [0x008F67C0]
(No symbol) [0x0094C4D8]
(No symbol) [0x0093A536]
(No symbol) [0x009182DC]
(No symbol) [0x009193DD]
GetHandleVerifier [0x00CBAABD+2539405]
GetHandleVerifier [0x00CFA78F+2800735]
GetHandleVerifier [0x00CF456C+2775612]
GetHandleVerifier [0x00AE51E0+616112]
(No symbol) [0x009F5F8C]
(No symbol) [0x009F2328]
(No symbol) [0x009F240B]
(No symbol) [0x009E4FF7]
BaseThreadInitThunk [0x779200C9+25]
RtlGetAppContainerNamedObjectPath [0x77C87B1E+286]
RtlGetAppContainerNamedObjectPath [0x77C87AEE+238]
Python:3.11.4
硒:4.10.0
网络驱动程序管理器:3.8.6
Chrome:115.0.5790.102(匹配ChromeDriver版本)
删除通过 webdriver.ChromeOptions()
实例添加的以下
参数,除非强制要求:
--disable-gpu
--no-sandbox
--disable-dev-shm-usage
--disable-software-rasterizer
ignore-certificate-errors
--remote-debugging-port=9222
此外,如果您使用 Selenium v4.6 或更高版本,则不必再显式使用 ChromeDriverManager().install(),因为 Selenium Manager 可以静默下载匹配ChromeDriver
您的最小代码块可以是:
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument("--start-maximized")
driver = webdriver.Chrome(options=option)
driver.get("https://google.com/")
删除 webdrivermanager 依赖项。它与内置的 selenium 管理器冲突。