TestDriver’s element finding system uses AI to locate elements on screen using natural language descriptions. The find() method returns an Element object that you can interact with.
Locate an element on screen using a natural language description.
Copy
const element = await testdriver.find(description)
Parameters:
description (string) - Natural language description of the element to find
Returns:Promise<Element> - Element instance that has been locatedExample:
Copy
// Find a buttonconst submitButton = await testdriver.find('the submit button');// Find an input field with contextconst emailField = await testdriver.find('email input field in the login form');// Find an element by visual characteristicsconst redButton = await testdriver.find('red button in the top right corner');
Be specific in your descriptions. Include visual details, location context, or nearby text to improve accuracy.
Re-locate the element, optionally with a new description.
Copy
await element.find(newDescription)
Parameters:
newDescription (string, optional) - New description to search for
Returns:Promise<Element> - This element instanceExample:
Copy
// Re-locate if the UI changedconst element = await testdriver.find('submit button');// ... page updates ...await element.find(); // Re-locate with same description// Or update the descriptionawait element.find('blue submit button'); // Now looking for blue button
Direct access to coordinate values. Always available after successful find().
Copy
element.x // Top-left X coordinate (number)element.y // Top-left Y coordinate (number)element.centerX // Center X coordinate (number)element.centerY // Center Y coordinate (number)
Example:
Copy
const button = await testdriver.find('submit button');console.log(`Button at: (${button.x}, ${button.y})`);console.log(`Button center: (${button.centerX}, ${button.centerY})`);// Use for custom mouse operationsawait testdriver.click(button.centerX, button.centerY);
Complete bounding box information including position and dimensions.
Copy
element.boundingBox
Returns:Object | null - Bounding box with all dimension data
Copy
{ x: number, // Top-left X y: number, // Top-left Y width: number, // Width in pixels height: number // Height in pixels}
Example:
Copy
const element = await testdriver.find('dialog box');if (element.boundingBox) { const { x, y, width, height } = element.boundingBox; console.log(`Dialog: ${width}x${height} at (${x}, ${y})`); // Calculate if element is in viewport const rightEdge = x + width; const bottomEdge = y + height; console.log(`Element extends to (${rightEdge}, ${bottomEdge})`);}
Element objects can be safely serialized using JSON.stringify() for logging, debugging, and data storage. Circular references are automatically removed:
Copy
const element = await testdriver.find('login button');// Safe to stringify - no circular reference errors!console.log(JSON.stringify(element, null, 2));
Serialized output includes:
Copy
{ "description": "login button", "coordinates": { "x": 100, "y": 200, "centerX": 150, "centerY": 225 }, "found": true, "threshold": 0.01, "x": 100, "y": 200, "cache": { "hit": true, "strategy": "pixel-diff", "createdAt": "2025-12-09T10:30:00.000Z", "diffPercent": 0.0023, "imageUrl": "https://cache.testdriver.ai/..." }, "similarity": 0.98, "confidence": 0.95, "selector": "button#login", "aiResponse": "Found the blue login button in the center of the form..."}
Serialized properties:
Property
Type
Description
description
string
Element search description
coordinates
object
Full coordinate object {x, y, centerX, centerY}
found
boolean
Whether element was located
threshold
number
Cache threshold used for this find
x, y
number
Top-left coordinates
cache.hit
boolean
Whether cache was used
cache.strategy
string
Cache strategy (e.g., “pixel-diff”)
cache.createdAt
string
ISO timestamp when cache was created
cache.diffPercent
number
Pixel difference from cached image
cache.imageUrl
string
URL to cached screenshot
similarity
number
Similarity score (0-1)
confidence
number
AI confidence score (0-1)
selector
string
CSS/XPath selector if available
aiResponse
string
AI’s explanation of what it found
Use cases:
Copy
// Debugging element detectionconst element = await testdriver.find('submit button');if (!element.found()) { console.error('Element not found:', JSON.stringify(element, null, 2));}// Logging cache performanceconst data = JSON.parse(JSON.stringify(element));if (data.cache.hit) { console.log(`Cache hit! Diff: ${(data.cache.diffPercent * 100).toFixed(2)}%`);}// Sharing element data across processesconst elementData = JSON.stringify(element);// Send to another process, log to file, etc.
Use JSON serialization when you need to log element data or when debugging why an element wasn’t found. The serialized output excludes large binary data (screenshots) and circular references.
// Element that moves or changesconst notification = await testdriver.find('success notification');// Do something that might cause it to moveawait testdriver.scroll('down', 300);// Re-locate the elementawait notification.find();if (notification.found()) { await notification.click();}
Include visual details, position context, and nearby text:
Copy
// ❌ Too vagueawait testdriver.find('button');// ✅ Specificawait testdriver.find('blue submit button below the email field');
Check if element was found
Always verify elements were located before interacting:
Copy
const element = await testdriver.find('submit button');if (!element.found()) { throw new Error('Submit button not found');}await element.click();
Use polling for dynamic content
Don’t assume elements are immediately visible:
Copy
let element;for (let i = 0; i < 30; i++) { element = await testdriver.find('loading complete message'); if (element.found()) break; await new Promise(resolve => setTimeout(resolve, 1000));}
Reuse element references when possible
If you need to interact with the same element multiple times, reuse the reference:
Copy
const input = await testdriver.find('search input');await input.click();await testdriver.type('first search');await testdriver.pressKeys(['enter']);// Re-use the same element referenceawait input.click();await testdriver.pressKeys(['ctrl', 'a']); // Select allawait testdriver.type('second search');