Most Selenium scripts die when a class name changes. AI agents tolerate structural shifts. They focus on the goal. This tutorial uses Agent-Browser to scrape product prices without writing CSS selectors. You will build a resilient scraper in under 15 minutes. Traditional automation requires constant maintenance. Agent-based approaches reduce this overhead by interpreting the DOM semantically.
Prerequisites
You need Node.js 18+ and a Chrome installation. You also need an API key for your LLM provider. Ensure your network allows outbound connections to the model endpoint. Local testing works best before deploying to CI. Do not skip the environment setup. Missing dependencies cause silent failures during execution.
Steps
- Install the package
npm install agent-browser. This pulls the core automation engine. Do not install older puppeteer wrappers. They lack the reasoning layer. Verify the installation by checking node_modules. Ensure you have the latest version to avoid known bugs in the viewport handler. Older versions may crash on dynamic content.- Configure the client
scraper.js. Import the agent class. Set your API key in the environment. Hardcoding keys causes leaks. Use .env files and load them with dotenv. Initialize the agent with a viewport size of 1920x1080. Mobile views often trigger different layouts that confuse the model. Desktop views remain consistent across most e-commerce platforms.- Define the objective
- Execute and capture
run() method. Wait for the promise to resolve. Log the output to the console. You should see a structured object containing the price. Close the browser instance immediately after to save resources. Lingering processes consume RAM and cost money. Add a finally block to ensure closure even if errors occur during extraction.Common pitfalls
* Infinite loops: Agents sometimes get stuck on popups. Add a timeout constraint of 30 seconds per step. Force close if the threshold is exceeded. Monitor the logs for repeated actions.
* Cost overrun: Each step costs tokens. Limit the maximum steps to 5 for simple tasks. Monitor your dashboard during development. Unexpected navigation spikes bills quickly.
* Anti-bot detection: Some sites block automated heads. Use stealth plugins or rotate user agents if you hit 403 errors. Residential proxies help here. Do not hammer the endpoint.
* Visual changes: If the layout shifts drastically, the agent might click the wrong button. Verify output schema strictly. Validate data types before saving. Reject outliers automatically.
Next step
Learn how to handle login walls without exposing credentials in [Secure Agent Authentication Patterns](https://example.com/auth).