|
19 | 19 |
|
20 | 20 | logger = logging.getLogger("uvicorn.error") |
21 | 21 |
|
| 22 | +_LANDING_PAGE_HTML = """\ |
| 23 | +<!DOCTYPE html> |
| 24 | +<html> |
| 25 | +<head><title>Browser</title></head> |
| 26 | +<body style="margin:0;display:flex;justify-content:center;align-items:center; |
| 27 | + height:100vh;background:#f0f0f0;font-family:sans-serif;"> |
| 28 | + <div style="text-align:center;"> |
| 29 | + <h1 style="font-size:28px;margin-bottom:24px;">Browser — Enter a URL to get started</h1> |
| 30 | + <form onsubmit="window.location=document.getElementById('url').value;return false;" |
| 31 | + style="display:flex;gap:8px;justify-content:center;"> |
| 32 | + <input id="url" type="text" |
| 33 | + placeholder="Enter a URL, e.g. https://example.com" |
| 34 | + style="width:500px;padding:12px 16px;font-size:18px;border:2px solid #ccc; |
| 35 | + border-radius:6px;" |
| 36 | + autofocus /> |
| 37 | + <button type="submit" |
| 38 | + style="padding:12px 24px;font-size:18px;background:#2563eb;color:white; |
| 39 | + border:none;border-radius:6px;cursor:pointer;"> |
| 40 | + Go |
| 41 | + </button> |
| 42 | + </form> |
| 43 | + </div> |
| 44 | +</body> |
| 45 | +</html> |
| 46 | +""" |
| 47 | + |
22 | 48 | Action = Union[ |
23 | 49 | Click, DoubleClick, Drag, Keypress, |
24 | 50 | Move, Screenshot, Scroll, Type, Wait, |
@@ -100,7 +126,7 @@ async def _ensure_page(self) -> Page: |
100 | 126 | device_scale_factor=1, |
101 | 127 | ) |
102 | 128 | self._page = await context.new_page() |
103 | | - await self._page.goto("about:blank") |
| 129 | + await self._page.set_content(_LANDING_PAGE_HTML, wait_until="load") |
104 | 130 | return self._page |
105 | 131 |
|
106 | 132 | async def screenshot(self) -> str: |
@@ -157,11 +183,17 @@ async def execute(self, action: Action) -> str: |
157 | 183 | async def close(self) -> None: |
158 | 184 | """Close the browser and clean up resources.""" |
159 | 185 | if self._browser: |
160 | | - await self._browser.close() |
| 186 | + try: |
| 187 | + await self._browser.close() |
| 188 | + except Exception: |
| 189 | + logger.debug("Browser already disconnected during close") |
161 | 190 | self._browser = None |
162 | 191 | self._page = None |
163 | 192 | if self._playwright: |
164 | | - await self._playwright.stop() |
| 193 | + try: |
| 194 | + await self._playwright.stop() |
| 195 | + except Exception: |
| 196 | + logger.debug("Playwright already stopped during close") |
165 | 197 | self._playwright = None |
166 | 198 |
|
167 | 199 |
|
|
0 commit comments