360 Secure SkillHub
360 安全技能中心

Connect Cdp Browser

该 Skill 用于连接外部已启动的浏览器实例:用户提供 Chrome DevTools 协议调试端口后,借助 Playwright 的 connect_over_cdp 复用已有上下文与页面,附带辅助脚本与直接调用示例;支持获取页面标题与地址、导航、截图、点击、输入等常见操作,并给出连接被拒绝、超时、浏览器被关闭等排查提示。

wociaozhongyunonghaole
wociaozhongyunonghaole
热度 29.36
最近更新 2026-03-30 14:31:49
分类:自动化依赖:代码执行 / 联网浏览搜索应用场景:自动化脚本 / 工作流

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add connect-cdp-browser
Metadata
name
connect-cdp-browser
description
Connect to an already running browser via Chrome DevTools Protocol (CDP) port. Use when the user provides a browser CDP debugging port (typically 9222 or similar) and wants to control an externally launched browser instance. This skill enables Playwright-based browser automation for pre-launched browsers without starting a new browser process.

Connect CDP Browser

Connect to an already running browser via its Chrome DevTools Protocol (CDP) debugging port.

Overview

This skill enables connecting to externally launched browser instances using Playwright's connect_over_cdp() method. Useful when:

  • A browser is launched by external software/tool
  • You need to connect to an existing browser without starting a new one
  • The browser exposes a CDP debugging port

Quick Start

When user provides a CDP port (e.g., "port 37194"):

  1. Use the provided script to connect:
python scripts/connect_cdp.py --port 37194
  1. Or execute Python code directly:
from playwright.sync_api import sync_playwright

port = 37194  # User-provided port
cdp_url = f"http://127.0.0.1:{port}"

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp(cdp_url)
    context = browser.contexts[0] if browser.contexts else browser.new_context()
    page = context.pages[0] if context.pages else context.new_page()
    
    print(f"Connected to browser")
    print(f"Page title: {page.title()}")
    print(f"Page URL: {page.url}")
    
    browser.close()

Common Operations

Get Page Information

print(f"Title: {page.title()}")
print(f"URL: {page.url}")
print(f"Content length: {len(page.content())}")

Navigate to URL

page.goto("https://example.com")

Take Screenshot

page.screenshot(path="screenshot.png", full_page=True)

Click Element

page.click("selector")

Type Text

page.fill("input[name='q']", "search query")

Troubleshooting

  • Connection refused: Ensure the browser is running and CDP port is correct
  • Timeout: Check if port is accessible from current environment
  • Browser closed: The browser must remain running during the session

Resources

scripts/

  • connect_cdp.py - Helper script for connecting to CDP browsers