User Agents

Web browsers and everyday web tech, explained

Handle Bot Detection and Captchas in Browser Automation

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Handling bot detection and captchas in browser automation allows scripts to interact with websites that employ anti-bot measures without being blocked or challenged repeatedly. This is essential for reliable web scraping, automated testing, and data collection tasks where headless browsers might otherwise be flagged. Success depends on mimicking real user behavior, managing browser fingerprints, and responding appropriately to captchas when they appear. Before starting, ensure you have a working automation framework like Puppeteer or Playwright, and that your use case complies with the target website’s terms of service.

What You'll Need

  • A browser automation library (e.g., Puppeteer, Playwright, or Selenium)
  • Node.js or Python environment (depending on your chosen tool)
  • Optional: third-party captcha-solving service (e.g., 2Captcha, Anti-Captcha)
  • Basic knowledge of JavaScript or Python
  • Access to a stable internet connection and target website(s)

Step-by-Step

  1. Use a real browser instead of headless mode: Start automation with a full browser instance (e.g., headless: false in Puppeteer). This reduces the chance of detection since headless browsers often exhibit detectable behaviors.
  2. Modify browser fingerprints: Websites detect automation by checking for inconsistencies in navigator properties, WebGL, fonts, and screen resolution. Use libraries like puppeteer-extra with StealthPlugin to patch common fingerprints automatically.
  3. Set realistic user agent strings: Rotate or set a user agent that matches a real browser and operating system. For example: userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'.
  4. Emulate human-like interactions: Add random delays between actions using setTimeout or sleep(). Avoid rapid, mechanical clicks or form submissions. Simulate mouse movements and slight typing delays when entering text.
  5. Manage cookies and sessions: Reuse authenticated sessions by saving and loading cookies. This avoids repeated logins, which can trigger bot detection.
  6. Handle iframes and shadow DOMs: Captchas like reCAPTCHA often load inside iframes. Use tools to detect and interact with these elements properly. In Puppeteer, use page.frames() to access iframe content.
  7. Detect captcha presence: Monitor page content for captcha indicators such as iframe[src*="recaptcha"], div.g-recaptcha, or challenge prompts. Use page.waitForSelector() to pause execution when a challenge appears.
  8. Integrate a captcha-solving service: For automated solving, send the site key and URL to a third-party service via their API. Retrieve the response token and inject it into the page using page.evaluate() to fill the hidden g-recaptcha-response field.
  9. Verify completion: After injecting the captcha response, trigger the form submission and monitor for success or a follow-up challenge. Some captchas require multiple rounds or behavioral analysis.
  10. Rotate IPs and sessions: Use rotating proxies to avoid IP-based rate limiting. Combine with session resets to simulate different users.
  11. Monitor and adapt: Regularly check for changes in captcha behavior or detection patterns. Update selectors, delays, and fingerprints as needed.

Common Mistakes

  • Running in default headless mode without stealth patches, making detection easy.
  • Using hardcoded, rapid-fire interactions that lack human variation.
  • Ignoring browser inconsistencies like missing plugins or WebGL fingerprints.
  • Submitting forms without properly triggering JavaScript events after filling captcha fields.
  • Reusing the same IP or session for high-volume requests, leading to blocks.
  • Assuming one-time captcha solving works permanently—some sites re-verify after inactivity.

Tips

  • Test automation scripts in a real browser first to observe natural behavior.
  • Use slowMo in Puppeteer or Playwright to simulate real-time input during development.
  • Randomize viewport sizes and screen dimensions across runs.
  • Enable JavaScript execution but disable images or CSS if not needed to speed up loading.
  • Cache successful captcha solutions temporarily for similar requests, if allowed.
  • Log detection events (e.g., challenge triggers) to analyze patterns and improve evasion.

Troubleshooting

If your script gets blocked despite precautions, first verify that stealth plugins are active and properly loaded. Check if the user agent and navigator properties match real browsers using browser fingerprinting tools. If captchas persist, ensure the solving service returned a valid token and that it was injected correctly into the DOM. Use page.content() to inspect if the g-recaptcha-response field is populated. If the site uses invisible reCAPTCHA, look for behavioral challenges triggered by mouse movement patterns. If automation fails intermittently, introduce exponential backoff and retry logic. For persistent IP blocks, switch to residential proxies. If detection evolves over time, update your automation setup to mimic newer browser versions and behaviors.

Frequently Asked Questions

Can I avoid captchas completely?

Not reliably. Some sites enforce captchas for suspicious traffic. The best approach is to reduce triggers through realistic behavior and fingerprint masking.

Are captcha-solving services legal?

Using them may violate a website’s terms of service. Always review the site’s policies and use such tools only in permitted contexts like testing.

Does Playwright handle stealth better than Puppeteer?

Both can be configured effectively. Playwright has built-in context isolation, but Puppeteer with stealth-plugin offers strong evasion when properly configured.

Why does reCAPTCHA appear even with a real browser?

Google’s system analyzes network signals, behavior, and history. Even real browsers get challenged if the IP or activity pattern seems automated.

Can I solve captchas manually during automation?

Yes—pause the script and wait for manual input. Use page.waitForFunction() to resume after a human solves the challenge.

How often should I rotate user agents?

Rotate across sessions, not mid-session. Use a pool of realistic user agents matching common browsers, but avoid excessive rotation that could raise suspicion.