|
| 1 | +/** |
| 2 | + * Text Search Demo - Using findTextRect() 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 clickRect() |
| 8 | + * 4. Handle multiple matches and filter by viewport visibility |
| 9 | + */ |
| 10 | + |
| 11 | +import { SentienceBrowser, findTextRect, clickRect } from '../src'; |
| 12 | + |
| 13 | +async function main() { |
| 14 | + const browser = new SentienceBrowser(); |
| 15 | + await browser.start(); |
| 16 | + |
| 17 | + const page = browser.getPage(); |
| 18 | + |
| 19 | + // Navigate to a search page |
| 20 | + await page.goto('https://www.google.com'); |
| 21 | + await page.waitForLoadState('networkidle'); |
| 22 | + |
| 23 | + console.log('\n' + '='.repeat(60)); |
| 24 | + console.log('Text Search Demo'); |
| 25 | + console.log('='.repeat(60) + '\n'); |
| 26 | + |
| 27 | + // Example 1: Simple text search |
| 28 | + console.log('Example 1: Finding "Google Search" button'); |
| 29 | + console.log('-'.repeat(60)); |
| 30 | + let result = await findTextRect(page, 'Google Search'); |
| 31 | + |
| 32 | + if (result.status === 'success' && result.results) { |
| 33 | + console.log(`✓ Found ${result.matches} match(es) for '${result.query}'`); |
| 34 | + for (let i = 0; i < Math.min(3, result.results.length); i++) { |
| 35 | + const match = result.results[i]; |
| 36 | + console.log(`\nMatch ${i + 1}:`); |
| 37 | + console.log(` Text: '${match.text}'`); |
| 38 | + console.log(` Position: (${match.rect.x.toFixed(1)}, ${match.rect.y.toFixed(1)})`); |
| 39 | + console.log(` Size: ${match.rect.width.toFixed(1)}x${match.rect.height.toFixed(1)} pixels`); |
| 40 | + console.log(` In viewport: ${match.in_viewport}`); |
| 41 | + console.log( |
| 42 | + ` Context: ...${match.context.before}[${match.text}]${match.context.after}...` |
| 43 | + ); |
| 44 | + } |
| 45 | + } else { |
| 46 | + console.log(`✗ Search failed: ${result.error}`); |
| 47 | + } |
| 48 | + |
| 49 | + // Example 2: Find and click search box (using simple string syntax) |
| 50 | + console.log('\n\nExample 2: Finding and clicking the search box'); |
| 51 | + console.log('-'.repeat(60)); |
| 52 | + result = await findTextRect(page, { |
| 53 | + text: 'Search', |
| 54 | + maxResults: 5 |
| 55 | + }); |
| 56 | + |
| 57 | + if (result.status === 'success' && result.results) { |
| 58 | + // Find the first visible match |
| 59 | + for (const match of result.results) { |
| 60 | + if (match.in_viewport) { |
| 61 | + console.log(`✓ Found visible match: '${match.text}'`); |
| 62 | + console.log(` Clicking at (${match.rect.x.toFixed(1)}, ${match.rect.y.toFixed(1)})`); |
| 63 | + |
| 64 | + // Click in the center of the text |
| 65 | + const clickResult = await clickRect(browser, { |
| 66 | + x: match.rect.x, |
| 67 | + y: match.rect.y, |
| 68 | + w: match.rect.width, |
| 69 | + h: match.rect.height |
| 70 | + }); |
| 71 | + |
| 72 | + if (clickResult.success) { |
| 73 | + console.log(` ✓ Click successful!`); |
| 74 | + } |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Example 3: Case-sensitive search |
| 81 | + console.log('\n\nExample 3: Case-sensitive search for "GOOGLE"'); |
| 82 | + console.log('-'.repeat(60)); |
| 83 | + const resultInsensitive = await findTextRect(page, { |
| 84 | + text: 'GOOGLE', |
| 85 | + caseSensitive: false |
| 86 | + }); |
| 87 | + const resultSensitive = await findTextRect(page, { |
| 88 | + text: 'GOOGLE', |
| 89 | + caseSensitive: true |
| 90 | + }); |
| 91 | + |
| 92 | + console.log(`Case-insensitive search: ${resultInsensitive.matches || 0} matches`); |
| 93 | + console.log(`Case-sensitive search: ${resultSensitive.matches || 0} matches`); |
| 94 | + |
| 95 | + // Example 4: Whole word search |
| 96 | + console.log('\n\nExample 4: Whole word search'); |
| 97 | + console.log('-'.repeat(60)); |
| 98 | + const resultPartial = await findTextRect(page, { |
| 99 | + text: 'Search', |
| 100 | + wholeWord: false |
| 101 | + }); |
| 102 | + const resultWhole = await findTextRect(page, { |
| 103 | + text: 'Search', |
| 104 | + wholeWord: true |
| 105 | + }); |
| 106 | + |
| 107 | + console.log(`Partial word match: ${resultPartial.matches || 0} matches`); |
| 108 | + console.log(`Whole word only: ${resultWhole.matches || 0} matches`); |
| 109 | + |
| 110 | + // Example 5: Get viewport information |
| 111 | + console.log('\n\nExample 5: Viewport and scroll information'); |
| 112 | + console.log('-'.repeat(60)); |
| 113 | + result = await findTextRect(page, 'Google'); |
| 114 | + if (result.status === 'success' && result.viewport) { |
| 115 | + console.log(`Viewport size: ${result.viewport.width}x${result.viewport.height}`); |
| 116 | + if ('scroll_x' in result.viewport && 'scroll_y' in result.viewport) { |
| 117 | + console.log(`Scroll position: (${result.viewport.scroll_x}, ${result.viewport.scroll_y})`); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + console.log('\n' + '='.repeat(60)); |
| 122 | + console.log('Demo complete!'); |
| 123 | + console.log('='.repeat(60) + '\n'); |
| 124 | + |
| 125 | + await browser.close(); |
| 126 | +} |
| 127 | + |
| 128 | +main().catch(console.error); |
0 commit comments