You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium与Python时Chrome打开网站后立即关闭的问题求助

问题分析与解决方案

浏览器启动后立即关闭的问题

版本匹配说明

你使用的Chrome版本110.0.5481.104和ChromeDriver版本110.0.5481.77是兼容的,主版本号一致即可,末尾小版本差异不影响,所以版本匹配不是导致浏览器闪退的原因。

解决方法

浏览器启动后立即关闭,是因为程序执行完代码后自动销毁了driver对象。可以通过以下两种方式解决:

  • 添加暂停代码:在driver.get()后加入暂停语句,让程序保持运行状态,避免driver被销毁:
from selenium import webdriver
import os
import time

os.environ['PATH'] = r"C:/Users/xxxx/Desktop/xxxx/Selenium/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome()
driver.get("https://phptravels.com/demo/")
time.sleep(10)  # 暂停10秒,可按需调整时长
driver.quit()  # 手动关闭浏览器和会话
  • 使用with语句管理生命周期:这种方式会自动处理driver的销毁,但如果需要保持浏览器打开,还是需要添加暂停:
from selenium import webdriver
import os
import time

os.environ['PATH'] = r"C:/Users/xxxx/Desktop/xxxx/Selenium/chromedriver_win32/chromedriver.exe"
with webdriver.Chrome() as driver:
    driver.get("https://phptravels.com/demo/")
    time.sleep(10)

USB错误提示处理

终端出现的USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)错误,是Chrome和本地USB设备(比如摄像头、外接键鼠等)的通信问题,不会影响Selenium的自动化操作。如果想消除这个提示,可以尝试:

  • 拔掉不必要的外接USB设备后重新运行
  • 更新USB设备的驱动程序
  • 添加Chrome启动参数禁用USB检测(不推荐,可能影响依赖USB的功能):
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
import time

os.environ['PATH'] = r"C:/Users/xxxx/Desktop/xxxx/Selenium/chromedriver_win32/chromedriver.exe"
chrome_options = Options()
chrome_options.add_argument('--disable-usb')
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://phptravels.com/demo/")
time.sleep(10)
driver.quit()

内容的提问来源于stack exchange,提问作者user15221508

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 16:39:21