text
stringlengths
0
1.47k
if name == "navigate":
url = str(args.get("url") or "").strip()
if not url:
raise BrowserToolError("Give a web address to open.")
if "://" not in url:
url = "https://" + url
why = await _allowed(s, url)
if why:
raise BrowserToolError(f"That address is {why}, so it cannot be opened.")
try:
await page.goto(url, wait_until="domcontentloaded")
except Exception as e: # noqa: BLE001
raise BrowserToolError(f"The page did not open: {str(e).splitlines()[0][:200]}") from None
return f"Opened {await _where(s)}\n\n{await _snapshot(s, 80)}"
if name == "get_url":
return await _where(s)
if name == "snapshot":
return f"{await _where(s)}\n\n{await _snapshot(s, args.get('max_elements') or SNAPSHOT_CAP)}"
if name == "extract_text":
selector = str(args.get("selector") or "").strip() or "body"
cap = max(200, min(int(args.get("max_chars") or TEXT_CAP), TEXT_CAP))
try:
text = await page.locator(selector).first.inner_text()
except Exception as e: # noqa: BLE001
raise BrowserToolError(f"No readable text at {selector!r}: {str(e).splitlines()[0][:160]}") from None
text = "\n".join(line.strip() for line in text.splitlines() if line.strip())
return text[:cap] + (f"\n… {len(text) - cap} more characters" if len(text) > cap else "")
if name == "click":
loc = _target(s, args)
await _mark(s, loc)
try:
await loc.click(timeout=15000)
except Exception as e: # noqa: BLE001
raise BrowserToolError(f"The click did not land: {str(e).splitlines()[0][:160]}") from None
await _settled(s)
return f"Clicked. Now at {await _where(s)}"
if name == "type":
loc = _target(s, args)
await _mark(s, loc)
text = str(args.get("text") or "")
try:
await loc.fill(text, timeout=15000)
if args.get("submit"):
await loc.press("Enter")
await _settled(s)
except Exception as e: # noqa: BLE001
raise BrowserToolError(f"Typing failed: {str(e).splitlines()[0][:160]}") from None
return f"Typed {len(text)} characters." + (f" Now at {await _where(s)}" if args.get("submit") else "")
if name == "press_key":
key = str(args.get("key") or "").strip()
if not key:
raise BrowserToolError("Say which key.")
await page.keyboard.press(key)
await _settled(s)
return f"Pressed {key}. Now at {await _where(s)}"
if name == "scroll":
px = max(50, min(int(args.get("pixels") or 600), 5000))
dy = -px if str(args.get("direction") or "down") == "up" else px
await page.mouse.wheel(0, dy)
return f"Scrolled {'up' if dy < 0 else 'down'} {abs(dy)} pixels."
if name == "wait_for":
text, selector = str(args.get("text") or "").strip(), str(args.get("selector") or "").strip()
ms = int(args.get("ms") or 0)
budget = int(CALL_CAP_S * 1000) - 2000
try:
if text:
await page.get_by_text(text).first.wait_for(timeout=budget)
return f"\"{text}\" is on the page."
if selector:
await page.locator(selector).first.wait_for(timeout=budget)
return f"{selector} is on the page."
except Exception: # noqa: BLE001
raise BrowserToolError("It did not appear within the wait.") from None
if ms > 0:
await asyncio.sleep(min(ms, budget) / 1000)
return f"Waited {min(ms, budget)} ms."
raise BrowserToolError("Say what to wait for: text, a selector or a number of milliseconds.")
if name == "screenshot":
try:
png = await page.screenshot(type="png", full_page=bool(args.get("full_page")))
except Exception as e: # noqa: BLE001
raise BrowserToolError(f"The screenshot failed: {str(e).splitlines()[0][:160]}") from None
await registry.bump(s.sid, screenshots=1)
return ("image", png, f"Screenshot of {await _where(s)}")
if name == "back":
try:
await page.go_back(wait_until="domcontentloaded")
except Exception as e: # noqa: BLE001
raise BrowserToolError(f"Could not go back: {str(e).splitlines()[0][:160]}") from None
return f"Back at {await _where(s)}"
if name == "list_tabs":
rows = []
for i, p in enumerate(s.context.pages):
try:
t = await p.title()
except Exception: # noqa: BLE001
t = ""
rows.append(f"{i}{' *' if p is s.page else ''} {p.url}" + (f" ({t})" if t else ""))
return "\n".join(rows) or "(no tabs)"
if name == "switch_tab":