Docs/SDK/Wait API

Wait API

Wait for elements to appear using semantic conditions. The wait_for() / waitFor() function automatically optimizes polling intervals based on API usage.

Basic Usage

from sentience import wait_for, click

# Wait for button to appear
result = wait_for(browser, "role=button text~'Submit'", timeout=5.0)
if result.found:
    print(f"Found after {result.duration_ms}ms")
    click(browser, result.element.id)

# Wait for clickable element
result = wait_for(browser, "clickable=true", timeout=10.0)

# Wait with custom interval (local processing)
result = wait_for(browser, "role=button", timeout=5.0, interval=0.5, use_api=False)

Parameters

Python:

TypeScript:

Returns

WaitResult with:

Semantic Wait Conditions

You can use any query selector to wait for elements:

# Wait for clickable element
wait_for(browser, "clickable=true")

# Wait for important element
wait_for(browser, "importance>100")

# Wait for visible link
wait_for(browser, "role=link visible=true")

# Wait for specific text
wait_for(browser, "text~'Loading complete'")

Auto-Optimized Polling

The wait function automatically adjusts polling intervals based on whether you're using the server API or local extension:

This ensures efficient waiting while minimizing API calls when using server-side processing.

Complete Example

from sentience import SentienceBrowser, snapshot, wait_for, click

with SentienceBrowser(api_key="sk_...") as browser:
    browser.page.goto("https://example.com")
    
    # Wait for page to load (wait for any clickable element)
    result = wait_for(browser, "clickable=true", timeout=10.0)
    if not result.found:
        print("Page failed to load")
        return
    
    # Take snapshot
    snap = snapshot(browser)
    
    # Wait for specific button to appear
    result = wait_for(browser, "role=button text~'Submit'", timeout=5.0)
    if result.found:
        click(browser, result.element.id)
    else:
        print("Button not found within timeout")