add09c2faa
10 funciones Go en infra/ para controlar Chrome via Chrome DevTools Protocol: chrome_launch, cdp_connect, cdp_navigate, cdp_evaluate, cdp_screenshot, cdp_click, cdp_type_text, cdp_wait_element, cdp_get_html, cdp_close. WebSocket RFC 6455 implementado sin dependencias externas. Incluye tests de integración con Chrome real. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
39 lines
982 B
Go
39 lines
982 B
Go
package infra
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// CdpWaitElement espera hasta que un selector CSS exista en el DOM.
|
|
// Hace polling con Runtime.evaluate hasta que el elemento aparezca o se agote el timeout.
|
|
// Retorna error si el timeout se agota sin encontrar el elemento.
|
|
func CdpWaitElement(c *CDPConn, selector string, timeout time.Duration) error {
|
|
if c == nil {
|
|
return fmt.Errorf("cdp wait element: conexion nula")
|
|
}
|
|
if timeout <= 0 {
|
|
timeout = 10 * time.Second
|
|
}
|
|
|
|
deadline := time.Now().Add(timeout)
|
|
interval := 200 * time.Millisecond
|
|
|
|
js := fmt.Sprintf(`document.querySelector(%q) !== null`, selector)
|
|
|
|
for time.Now().Before(deadline) {
|
|
result, err := CdpEvaluate(c, js)
|
|
if err != nil {
|
|
// Error en evaluate — puede que la pagina aun no este lista
|
|
time.Sleep(interval)
|
|
continue
|
|
}
|
|
if result == "true" {
|
|
return nil
|
|
}
|
|
time.Sleep(interval)
|
|
}
|
|
|
|
return fmt.Errorf("cdp wait element: selector %q no encontrado despues de %s", selector, timeout)
|
|
}
|