Skip to content

Commit 6cf2187

Browse files
committed
feat: wait before screenshots
1 parent 5ec9544 commit 6cf2187

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/playwright_computer_use/async_api.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,25 @@ def to_params(self) -> BetaToolComputerUse20241022Param:
192192
"""Params describing the tool. Used by Claude to understand this is a computer use tool."""
193193
return {"name": self.name, "type": self.api_type, **self.options}
194194

195-
def __init__(self, page: Page, use_cursor: bool = True):
195+
def __init__(
196+
self,
197+
page: Page,
198+
use_cursor: bool = True,
199+
screenshot_wait_until: Literal["load", "domcontentloaded", "networkidle"]
200+
| None = None,
201+
):
196202
"""Initializes the PlaywrightComputerTool.
197203
198204
Args:
199205
page: The Async Playwright page to interact with.
200206
use_cursor: Whether to display the cursor in the screenshots or not.
207+
screenshot_wait_until: Optional, wait until the page is in a specific state before taking a screenshot. Default does not wait
201208
"""
202209
super().__init__()
203210
self.page = page
204211
self.use_cursor = use_cursor
205212
self.mouse_position: tuple[int, int] = (0, 0)
213+
self.screenshot_wait_until = screenshot_wait_until
206214

207215
async def __call__(
208216
self,
@@ -284,6 +292,9 @@ async def __call__(
284292

285293
async def screenshot(self) -> ToolResult:
286294
"""Take a screenshot of the current screen and return the base64 encoded image."""
295+
if self.screenshot_wait_until is not None:
296+
await self.page.wait_for_timeout(self.screenshot_wait_until)
297+
await self.page.wait_for_load_state()
287298
screenshot = await self.page.screenshot()
288299
image = Image.open(io.BytesIO(screenshot))
289300
img_small = image.resize((self.width, self.height), Image.LANCZOS)

0 commit comments

Comments
 (0)