You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Selenium 4.12 如何连接已存在的Chrome浏览器会话?

Selenium 4.12 如何连接已存在的Chrome浏览器会话?

嗨,我来帮你搞定这个问题!要在Selenium 4.12里复用已打开的Chrome会话(比如脚本报错后残留的浏览器窗口),核心是要先让Chrome以远程调试模式启动,再用脚本对接它。你之前尝试的debuggerAddress方法其实在Selenium 4里依然有效,只是可能没做好前置步骤,具体操作如下:

第一步:确保目标Chrome会话以调试模式启动

不管是通过脚本启动,还是手动启动Chrome,都必须让它开启远程调试端口,否则后续没法连接。

方式1:修改你的原脚本,启动带调试端口的Chrome

在你原来启动浏览器的代码里,给Options添加两个关键参数:

options = Options()
# 开启远程调试端口,这里用9222(你也可以换其他未被占用的端口)
options.add_argument("--remote-debugging-port=9222")
# 指定一个独立的用户数据目录,避免和你日常使用的Chrome会话冲突
options.add_argument(r"--user-data-dir=C:\ChromeDebugProfile")

# 保留你原来的其他配置
preferences = {
    "download.default_directory": r"C:\................",
    "safebrowsing.enabled": True,
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "profile.password_manager_enabled": False,
    "credentials_enable_service": False,
}
options.add_argument("--allow-running-insecure-content")
options.add_argument("--disable-web-security")
options.add_argument("--disable-notifications")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("prefs", preferences)

# 正常启动浏览器
service = Service()
driver = webdriver.Chrome(service=service, options=options)
driver.get("https://stackoverflow.com/")

这样一来,即使脚本运行出错、浏览器窗口残留,这个窗口也会在9222端口监听调试请求,方便后续连接。

方式2:手动启动带调试端口的Chrome

如果是要连接手动打开的Chrome,先关闭所有Chrome窗口,然后通过命令行启动:

"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --user-data-dir="C:\ChromeDebugProfile"

(路径根据你Chrome的实际安装位置调整)

第二步:编写连接已存在会话的脚本

当目标Chrome窗口在9222端口运行时,就可以用以下脚本连接它了(注意不要调用driver.get(),否则会覆盖当前页面):

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

options = Options()
# 关键:指定要连接的调试地址
options.add_experimental_option("debuggerAddress", "localhost:9222")

# 保留你需要的配置(可选,因为已有会话的配置已经生效)
preferences = {
    "download.default_directory": r"C:\................",
    "safebrowsing.enabled": True,
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "profile.password_manager_enabled": False,
    "credentials_enable_service": False,
}
options.add_argument("--allow-running-insecure-content")
options.add_argument("--disable-web-security")
options.add_argument("--disable-notifications")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("prefs", preferences)

service = Service()
# 直接连接已有的会话,无需重新打开浏览器
driver = webdriver.Chrome(service=service, options=options)

# 现在可以直接操作当前页面了,比如打印标题验证连接成功
print("当前页面标题:", driver.title)
# 接下来就可以调试之前出错的元素或步骤了

常见问题说明

  • 为什么之前尝试没成功?大概率是你最初启动的Chrome没有开启--remote-debugging-port参数,导致浏览器没有监听调试请求,自然没法连接。
  • 必须用--user-data-dir吗?是的,因为如果已有Chrome实例在运行,新启动的调试模式Chrome会共享默认会话,可能导致端口监听失败,用独立目录可以避免这个问题。
  • 连接后不要调用driver.get():否则会跳转到新页面,覆盖你原来的错误页面,失去调试意义。

备注:内容来源于stack exchange,提问作者A_Harris

火山引擎 最新活动