Learn how to wait for elements to appear, disappear, or change state using Sentience's intelligent waiting system. No more arbitrary sleep() delays or flaky tests.
Web pages load asynchronously. Forms submit in the background. Modals fade in. Traditional approaches use fixed delays that are either too short (causing failures) or too long (wasting time).
# Bad: arbitrary sleep
button.click()
time.sleep(5) # Too long or too short?
next_element.click() # Might not exist yet!
# Good: wait for semantic condition
click(browser, submit_btn.id)
result = wait_for(browser, "role=heading text~'Success'", timeout=10.0)
if result.found:
print("Form submitted successfully!")
Wait for an element matching a semantic query to appear:
from sentience import wait_for
# Wait for success message
result = wait_for(browser, "role=heading text~'Success'", timeout=10.0)
# Check result
if result.found:
print(f"Found: {result.element.text}")
else:
print("Element not found within timeout")
# Wait for loading to disappear
result = wait_for(
browser,
"role=status text~'Loading'",
timeout=30.0,
wait_for_disappear=True
)Parameters:
query - Semantic query stringtimeout - Maximum wait time in secondswait_for_disappear - Wait for element to disappear (optional)poll_interval - How often to check (default: 0.5s)Returns:
found - true if element was found/disappearedelement - The found element (if found=true)elapsed_time - Time takenWait for a custom condition using a predicate function:
from sentience import snapshot, find, wait_for_condition
def has_items_in_cart():
snap = snapshot(browser)
cart_count = find(snap, "role=status text~'items'")
if cart_count:
# Extract number from text like "3 items"
count = int(cart_count.text.split()[0])
return count > 0
return False
# Wait until cart has items
success = wait_for_condition(browser, has_items_in_cart, timeout=10.0)
if success:
print("Cart has items!")from sentience import SentienceBrowser, wait_for
with SentienceBrowser(api_key="sk_...") as browser:
browser.page.goto("https://example.com")
# Wait for main content to load
result = wait_for(browser, "role=main", timeout=10.0)
if result.found:
print("Page loaded successfully!")
else:
print("Page failed to load")from sentience import snapshot, find, click, wait_for
# Submit form
snap = snapshot(browser)
submit_btn = find(snap, "role=button text~'submit'")
click(browser, submit_btn.id)
# Wait for success or error message
success = wait_for(browser, "text~'success'", timeout=10.0)
error = wait_for(browser, "text~'error'", timeout=1.0)
if success.found:
print("Form submitted successfully!")
elif error.found:
print(f"Form error: {error.element.text}")
else:
print("Form submission status unknown")from sentience import click, wait_for
# Click button that triggers loading
click(browser, button.id)
# Wait for spinner to appear
wait_for(browser, "role=status text~'loading'", timeout=2.0)
# Wait for spinner to disappear
result = wait_for(
browser,
"role=status text~'loading'",
timeout=30.0,
wait_for_disappear=True
)
if result.found:
print("Loading complete!")from sentience import click, wait_for, find, snapshot
# Open modal
snap = snapshot(browser)
open_btn = find(snap, "role=button text~'open'")
click(browser, open_btn.id)
# Wait for modal
modal = wait_for(browser, "role=dialog", timeout=5.0)
if modal.found:
# Interact with modal
snap = snapshot(browser)
close_btn = find(snap, "role=button text~'close'")
click(browser, close_btn.id)from sentience import snapshot, query, wait_for_condition
def has_search_results():
snap = snapshot(browser)
results = query(snap, "role=article")
return len(results) > 0
# Search and wait for results
type_text(browser, search_box.id, "wireless mouse")
press_key(browser, "Enter")
success = wait_for_condition(browser, has_search_results, timeout=10.0)
if success:
snap = snapshot(browser)
results = query(snap, "role=article")
print(f"Found {len(results)} results")# Short timeout for expected fast operations
wait_for(browser, "role=alert", timeout=2.0)
# Longer timeout for slow operations
wait_for(browser, "text~'Processing complete'", timeout=60.0)
# Good - check if element was found
result = wait_for(browser, "role=button text~'Next'", timeout=5.0)
if result.found:
click(browser, result.element.id)
else:
print("Next button not found")
# Avoid - assuming success
result = wait_for(browser, "role=button text~'Next'", timeout=5.0)
click(browser, result.element.id) # Might crash if not found!
try:
result = wait_for(browser, "role=heading text~'Success'", timeout=10.0)
if result.found:
print("Operation successful!")
else:
print("Success message not found")
except Exception as e:
print(f"Error while waiting: {e}")
# Good - semantic query (survives redesigns)
wait_for(browser, "role=button text~'continue'", timeout=5.0)
# Avoid - ID-based (breaks on changes)
wait_for(browser, "id=btn-continue-2024", timeout=5.0)
| Parameter | Type | Description | Default |
|---|---|---|---|
query | string | Semantic query | Required |
timeout | number | Max wait time (seconds) | Required |
wait_for_disappear | boolean | Wait for disappearance | false |
poll_interval | number | Check interval (seconds) | 0.5 |
| Parameter | Type | Description | Default |
|---|---|---|---|
condition | function | Predicate function | Required |
timeout | number | Max wait time (seconds) | Required |
poll_interval | number | Check interval (seconds) | 0.5 |