|
| 1 | +""" |
| 2 | +Text Search Demo - Using find_text_rect() to locate elements by visible text |
| 3 | +
|
| 4 | +This example demonstrates how to: |
| 5 | +1. Find text on a webpage and get exact pixel coordinates |
| 6 | +2. Use case-sensitive and whole-word matching options |
| 7 | +3. Click on found text using click_rect() |
| 8 | +4. Handle multiple matches and filter by viewport visibility |
| 9 | +""" |
| 10 | + |
| 11 | +from sentience import SentienceBrowser, click_rect, find_text_rect |
| 12 | + |
| 13 | + |
| 14 | +def main(): |
| 15 | + with SentienceBrowser() as browser: |
| 16 | + # Navigate to a search page |
| 17 | + browser.page.goto("https://www.google.com") |
| 18 | + browser.page.wait_for_load_state("networkidle") |
| 19 | + |
| 20 | + print("\n" + "=" * 60) |
| 21 | + print("Text Search Demo") |
| 22 | + print("=" * 60 + "\n") |
| 23 | + |
| 24 | + # Example 1: Simple text search |
| 25 | + print("Example 1: Finding 'Google Search' button") |
| 26 | + print("-" * 60) |
| 27 | + result = find_text_rect(browser, "Google Search") |
| 28 | + |
| 29 | + if result.status == "success" and result.results: |
| 30 | + print(f"✓ Found {result.matches} match(es) for '{result.query}'") |
| 31 | + for i, match in enumerate(result.results[:3]): # Show first 3 |
| 32 | + print(f"\nMatch {i + 1}:") |
| 33 | + print(f" Text: '{match.text}'") |
| 34 | + print(f" Position: ({match.rect.x:.1f}, {match.rect.y:.1f})") |
| 35 | + print(f" Size: {match.rect.width:.1f}x{match.rect.height:.1f} pixels") |
| 36 | + print(f" In viewport: {match.in_viewport}") |
| 37 | + print(f" Context: ...{match.context.before}[{match.text}]{match.context.after}...") |
| 38 | + else: |
| 39 | + print(f"✗ Search failed: {result.error}") |
| 40 | + |
| 41 | + # Example 2: Find and click search box |
| 42 | + print("\n\nExample 2: Finding and clicking the search box") |
| 43 | + print("-" * 60) |
| 44 | + result = find_text_rect(browser, "Search", max_results=5) |
| 45 | + |
| 46 | + if result.status == "success" and result.results: |
| 47 | + # Find the first visible match |
| 48 | + for match in result.results: |
| 49 | + if match.in_viewport: |
| 50 | + print(f"✓ Found visible match: '{match.text}'") |
| 51 | + print(f" Clicking at ({match.rect.x:.1f}, {match.rect.y:.1f})") |
| 52 | + |
| 53 | + # Click in the center of the text |
| 54 | + click_result = click_rect( |
| 55 | + browser, |
| 56 | + { |
| 57 | + "x": match.rect.x, |
| 58 | + "y": match.rect.y, |
| 59 | + "w": match.rect.width, |
| 60 | + "h": match.rect.height, |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + if click_result.success: |
| 65 | + print(f" ✓ Click successful!") |
| 66 | + break |
| 67 | + |
| 68 | + # Example 3: Case-sensitive search |
| 69 | + print("\n\nExample 3: Case-sensitive search for 'GOOGLE'") |
| 70 | + print("-" * 60) |
| 71 | + result_insensitive = find_text_rect(browser, "GOOGLE", case_sensitive=False) |
| 72 | + result_sensitive = find_text_rect(browser, "GOOGLE", case_sensitive=True) |
| 73 | + |
| 74 | + print(f"Case-insensitive search: {result_insensitive.matches or 0} matches") |
| 75 | + print(f"Case-sensitive search: {result_sensitive.matches or 0} matches") |
| 76 | + |
| 77 | + # Example 4: Whole word search |
| 78 | + print("\n\nExample 4: Whole word search") |
| 79 | + print("-" * 60) |
| 80 | + result_partial = find_text_rect(browser, "Search", whole_word=False) |
| 81 | + result_whole = find_text_rect(browser, "Search", whole_word=True) |
| 82 | + |
| 83 | + print(f"Partial word match: {result_partial.matches or 0} matches") |
| 84 | + print(f"Whole word only: {result_whole.matches or 0} matches") |
| 85 | + |
| 86 | + # Example 5: Get viewport information |
| 87 | + print("\n\nExample 5: Viewport and scroll information") |
| 88 | + print("-" * 60) |
| 89 | + result = find_text_rect(browser, "Google") |
| 90 | + if result.status == "success" and result.viewport: |
| 91 | + print(f"Viewport size: {result.viewport.width}x{result.viewport.height}") |
| 92 | + # Note: scroll position would be available if viewport had scroll_x/scroll_y fields |
| 93 | + |
| 94 | + print("\n" + "=" * 60) |
| 95 | + print("Demo complete!") |
| 96 | + print("=" * 60 + "\n") |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments