You need to enable JavaScript to run this app. Python Selenium设置detach后浏览器仍自动关闭如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

Python Selenium设置detach后浏览器仍自动关闭如何解决?

问题根因
  • 你使用的seleniumwire.webdriver.Chrome和原生Selenium的参数校验逻辑不同,直接将驱动路径作为第一个位置参数传入,会导致后续传入的options参数未被正确识别,detach配置没有生效。
  • 代码执行完driver.get(url)后无后续阻塞逻辑,Python脚本运行结束进程退出时,会自动终止关联的Chrome进程。
  • 部分低版本seleniumwire对detach实验选项的适配存在缺陷,无法正常生效。
修复方案
  1. 调整驱动初始化写法,显式指定参数名,补充反检测配置避免网站触发进程终止:
from bs4 import BeautifulSoup
import requests
from urllib.request import urlopen
import json
import time
from seleniumwire import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementNotVisibleException

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"])
chrome_options.add_argument("--disable-blink-features=AutomationControlled")

search_key = '성남 스터디카페'
url = "https://map.naver.com/v5/search/" + search_key
driverPath = "chromedriver.exe"

# 显式指定executable_path参数,确保options被正确加载
driver = webdriver.Chrome(executable_path=driverPath, options=chrome_options)
driver.get(url)
  1. 若调整后仍存在自动关闭问题,在代码末尾添加阻塞逻辑,避免脚本执行完毕直接退出:
# 放在代码最后一行,脚本会一直等待直到你在终端输入回车再结束
input()
# 也可以用time.sleep设置指定时长的阻塞,比如设置10分钟后自动退出:
# import time
# time.sleep(600)
  1. 若以上方案都无效,升级seleniumwire到适配Chrome91的兼容版本:
    pip install seleniumwire==4.6.4

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 10:45:03