The Agentic Scraper enables AI-powered browser automation for complex interactions like form filling, clicking buttons, and navigating multi-step workflows.
import { agenticScraper, getAgenticScraperRequest } from 'scrapegraph-js';
const apiKey = 'your-api-key';
const url = 'https://dashboard.scrapegraphai.com/';
const steps = [
'Type email@gmail.com in email input box',
'Type test-password@123 in password inputbox',
'click on login'
];
// Submit automation request
const response = await agenticScraper(apiKey, url, steps, true);
console.log('Request ID:', response.request_id);
// Check results
const result = await getAgenticScraperRequest(apiKey, response.request_id);
console.log('Status:', result.status);Performs automated browser actions on a webpage.
Parameters:
apiKey(string): Your ScrapeGraph AI API keyurl(string): The URL of the webpage to interact withsteps(string[]): Array of automation steps to performuseSession(boolean, optional): Whether to use session management (default: true)
Returns: Promise with request_id and initial status
Example Steps:
const steps = [
'click on search bar',
'type "laptop" in search input',
'press Enter key',
'wait for 2 seconds',
'click on first result',
'scroll down to reviews'
];Retrieves the status or result of an agentic scraper request.
Parameters:
apiKey(string): Your ScrapeGraph AI API keyrequestId(string): The request ID from a previous agentic scraper call
Returns: Promise with:
status: 'pending', 'completed', or 'failed'result: Automation results (when completed)error: Error message (when failed)created_at: Request creation timestampcompleted_at: Completion timestamp (when completed)
const loginSteps = [
'click on email input',
'type "user@example.com" in email field',
'click on password input',
'type "password123" in password field',
'click login button',
'wait for dashboard to load'
];
const response = await agenticScraper(apiKey, 'https://app.example.com/login', loginSteps, true);const shoppingSteps = [
'click on search bar',
'type "wireless headphones" in search',
'press Enter',
'wait for results to load',
'click on first product',
'click add to cart button',
'click view cart'
];
const response = await agenticScraper(apiKey, 'https://shop.example.com', shoppingSteps, true);const formSteps = [
'click on name input',
'type "John Doe" in name field',
'click on email input',
'type "john@example.com" in email field',
'click on message textarea',
'type "Hello, this is a test message" in message area',
'click submit button'
];
const response = await agenticScraper(apiKey, 'https://example.com/contact', formSteps, false);async function waitForCompletion(requestId, timeoutSeconds = 120) {
const startTime = Date.now();
const timeout = timeoutSeconds * 1000;
while (Date.now() - startTime < timeout) {
const status = await getAgenticScraperRequest(apiKey, requestId);
if (status.status === 'completed') {
return status.result;
} else if (status.status === 'failed') {
throw new Error(status.error);
}
await new Promise(resolve => setTimeout(resolve, 5000)); // Wait 5 seconds
}
throw new Error('Timeout waiting for completion');
}try {
const response = await agenticScraper(apiKey, url, steps, true);
const result = await waitForCompletion(response.request_id);
console.log('Automation successful:', result);
} catch (error) {
if (error.message.includes('validation')) {
console.log('Input validation failed:', error.message);
} else if (error.message.includes('timeout')) {
console.log('Automation timed out');
} else {
console.log('Automation failed:', error.message);
}
}Steps should be written in natural language describing the action to perform:
"click on login button""click on search icon""click on first result"
"type 'username' in email field""type 'password123' in password input""type 'search query' in search box"
"press Enter key""press Tab key""press Escape key"
"wait for 2 seconds""wait for page to load""wait for results to appear"
"scroll down""scroll to bottom""scroll to top"
- Use Session Management: Set
useSession: truefor multi-step workflows - Add Wait Steps: Include wait times between actions for reliability
- Be Specific: Use descriptive selectors like "login button" vs "button"
- Handle Timeouts: Implement proper timeout handling for long operations
- Validate Inputs: Check URLs and steps before making requests
// ❌ Invalid URL
await agenticScraper(apiKey, 'not-a-url', steps);
// ❌ Empty steps
await agenticScraper(apiKey, url, []);
// ❌ Invalid step
await agenticScraper(apiKey, url, ['click button', '']); // Empty step- Element not found: Make steps more specific or add wait times
- Timeout: Increase polling timeout or break down complex steps
- Session expired: Use session management for multi-step flows
curl --location 'https://api.scrapegraphai.com/v1/agentic-scrapper' \
--header 'SGAI-APIKEY: your-api-key' \
--header 'Content-Type: application/json' \
--data-raw '{
"url": "https://dashboard.scrapegraphai.com/",
"use_session": true,
"steps": [
"Type email@gmail.com in email input box",
"Type test-password@123 in password inputbox",
"click on login"
]
}'Check out the example files in the /examples directory:
agenticScraper_example.js- Basic usagegetAgenticScraperRequest_example.js- Status checkingagenticScraper_complete_example.js- Complete workflowagenticScraper_advanced_example.js- Advanced patterns with error handling
- Start with simple steps and gradually add complexity
- Test individual steps before combining them
- Use browser developer tools to identify element selectors
- Consider mobile vs desktop layouts when writing steps
- Monitor request status regularly for long-running automations