Docs/SDK/Screenshot API

Screenshot API

Capture screenshots of the current page and search text to find its pixel coordinate.

screenshot() - Capture Screenshot

Captures a screenshot of the current page.

from sentience import screenshot
import base64

# Capture PNG screenshot
data_url = screenshot(browser, format="png")

# Save to file
image_data = base64.b64decode(data_url.split(",")[1])
with open("screenshot.png", "wb") as f:
    f.write(image_data)

# JPEG with quality control
data_url = screenshot(browser, format="jpeg", quality=85)

Parameters:

Returns: Base64-encoded data URL string

find_text_rect() / findTextRect() - Search text to get its pixel coordinates

Finds all occurrences of text on the page and returns their exact pixel coordinates. This is useful for locating UI elements by their visible text content without needing element IDs or selectors.

from sentience import find_text_rect, click_rect

with SentienceBrowser() as browser:
    browser.page.goto("https://example.com")

    # Find "Sign In" button
    result = find_text_rect(browser, "Sign In")
    if result.status == "success" and result.results:
        first_match = result.results[0]
        print(f"Found at: ({first_match.rect.x}, {first_match.rect.y})")
        print(f"Size: {first_match.rect.width}x{first_match.rect.height}")
        print(f"In viewport: {first_match.in_viewport}")

    # Case-sensitive search
    result = find_text_rect(browser, "LOGIN", case_sensitive=True)

    # Whole word only (won't match "login" as part of "loginButton")
    result = find_text_rect(browser, "log", whole_word=True)

    # Find and click the first visible match
    result = find_text_rect(browser, "Buy Now", max_results=5)
    if result.status == "success" and result.results:
        for match in result.results:
            if match.in_viewport:
                # Click in the center of the text
                click_rect(browser, {
                    "x": match.rect.x,
                    "y": match.rect.y,
                    "w": match.rect.width,
                    "h": match.rect.height
                })
                break

Parameters:

Returns: TextRectSearchResult with:

Use Cases:

Notes: