--- name: cdp_get_ax_tree kind: pipeline lang: py domain: pipelines version: "1.0.0" purity: impure signature: "def cdp_get_ax_tree(debug_port: int, tab_id: str, depth: int = -1) -> list[dict]" description: "Conecta a Chrome via CDP WebSocket, habilita Accessibility y devuelve el AX tree completo del tab indicado. Usa websocket-client si está disponible, sino websockets async." tags: [navegator, cdp, chrome, browser, accessibility, ax-tree] uses_functions: [trim_ax_tree_py_core, chunk_ax_tree_py_core] uses_types: [] returns: [] returns_optional: false error_type: "error_go_core" imports: [json, threading, urllib.request, urllib.error, websocket] params: - name: debug_port desc: "Puerto de debug remoto de Chrome (ej. 9222). Lanzar Chrome con --remote-debugging-port=9222." - name: tab_id desc: "ID del tab CDP obtenido via GET /json/list (campo 'id'). Usar cdp_list_tabs_go_browser para listarlo." - name: depth desc: "Profundidad del árbol a obtener. -1 = completo (default)." output: "Lista de AXNode en formato CDP. Lista vacía si la página no tiene contenido accesible." tested: false tests: [] test_file_path: "" file_path: "python/functions/pipelines/cdp_get_ax_tree.py" --- ## Ejemplo ```python import urllib.request, json from pipelines.cdp_get_ax_tree import cdp_get_ax_tree from core.trim_ax_tree import trim_ax_tree from core.chunk_ax_tree import chunk_ax_tree # 1. Listar tabs para obtener tab_id with urllib.request.urlopen("http://127.0.0.1:9222/json/list") as r: tabs = json.loads(r.read()) tab_id = tabs[0]["id"] # 2. Obtener AX tree nodes = cdp_get_ax_tree(debug_port=9222, tab_id=tab_id) # 3. Reducir y chunkear para LLM trimmed = trim_ax_tree(nodes) chunks = chunk_ax_tree(trimmed, max_chars=25000) print(f"{len(nodes)} nodos → {len(trimmed)} trimmed → {len(chunks)} chunks") ``` ## Cuando usarla Cuando necesitas obtener el árbol de accesibilidad de una página Chrome ya abierta para procesarlo con un LLM o para automatización accesible (más estable que selectores CSS). Requiere Chrome lanzado con `--remote-debugging-port=PORT`. ## Gotchas - Chrome debe estar corriendo con `--remote-debugging-port=` y `--no-sandbox` en CI. - En WSL2 usar `--remote-debugging-address=0.0.0.0` y conectar al IP del host Windows, no a 127.0.0.1. - El tab no puede tener otro debugger adjunto (DevTools abierto) — cierra DevTools antes de llamar. - `Accessibility.getFullAXTree` puede tardar 2-5s en páginas grandes. - Timeout total de 15s — aumentar si la página es muy pesada. - Tests automáticos requieren Chrome corriendo. Para probar manualmente: ```bash # Lanzar Chrome en WSL2 chrome.exe --remote-debugging-port=9222 --headless=new https://example.com # Verificar curl http://127.0.0.1:9222/json/list | python3 -m json.tool # Ejecutar python3 -c " import json, urllib.request from pipelines.cdp_get_ax_tree import cdp_get_ax_tree with urllib.request.urlopen('http://127.0.0.1:9222/json/list') as r: tabs = json.loads(r.read()) nodes = cdp_get_ax_tree(9222, tabs[0]['id']) print(f'{len(nodes)} nodos') " ```