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

Windows11下使用Python Selenium Chromedriver无法打开网页求助

解决Selenium Chrome浏览器瞬间关闭的问题

核心原因

Python脚本执行完成后,webdriver.Chrome()创建的driver对象会被自动销毁,触发Selenium关闭浏览器进程,这就是你看到浏览器瞬间打开又关闭的原因。

可行解决办法

  • 添加阻塞逻辑,让脚本持续运行
    在driver.get()之后加入等待代码,阻止脚本立即结束:

    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome()
    driver.get("http://www.google.com")
    time.sleep(10)  # 固定等待10秒,可按需调整时长
    driver.quit()  # 手动关闭浏览器和驱动
    

    或者用用户交互等待,更灵活:

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get("http://www.google.com")
    input("按回车键关闭浏览器...")  # 等待用户输入后再关闭浏览器
    driver.quit()
    
  • 启用Chrome的detach模式
    通过配置ChromeOptions,让浏览器在脚本结束后保持打开状态:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_experimental_option("detach", True)
    
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("http://www.google.com")
    

    这种方式下,脚本执行完毕后浏览器不会自动关闭,需手动关闭窗口。

  • 排查代码退出逻辑
    检查你的脚本是否存在提前终止的代码(比如sys.exit()、意外的return),或者在IDE运行时是否开启了自动清理进程的选项。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 08:53:11