Handling CAPTCHA with Playwright
🚫 Important Disclaimer
Bypassing CAPTCHAs without permission may violate terms of service. Always ensure your automation is legal and ethical, especially for web scraping.
🔍 1. Detecting and Avoiding CAPTCHAs
Before attempting to solve them, try to avoid triggering them:
Use realistic user-agents
Add random delays (to simulate human interaction)
Avoid rapid or repetitive access
Rotate IPs (with proxies or VPNs)
Enable JavaScript (CAPTCHAs often appear when JS is disabled)
ts
Copy
Edit
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: false }); // use headful mode
const context = await browser.newContext({
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)...',
locale: 'en-US',
});
const page = await context.newPage();
await page.goto('https://example.com');
})();
🧠 2. Solving CAPTCHAs Automatically (When Permissible)
🤖 a. Image-Based CAPTCHAs (e.g., reCAPTCHA v2 Checkbox)
Use external CAPTCHA-solving services:
2Captcha, Anti-Captcha, CapSolver, etc.
Integrate using their API
Example: 2Captcha with reCAPTCHA
ts
Copy
Edit
const axios = require('axios');
const response = await axios.post('http://2captcha.com/in.php', {
key: 'YOUR_API_KEY',
method: 'userrecaptcha',
googlekey: 'SITE_KEY',
pageurl: 'https://example.com',
});
// Then poll for the result
After solving, inject the response into the g-recaptcha-response textarea:
js
Copy
Edit
await page.evaluate(`document.getElementById("g-recaptcha-response").innerHTML="${captchaSolution}"`);
⚠️ b. reCAPTCHA v3 or Invisible reCAPTCHA
These monitor behavior, so mimic human-like actions carefully. Services like 2Captcha support this too, but success rates vary.
🎨 3. Use Browser Plugins or Human-in-the-Loop
If fully automatic handling fails:
Integrate human solvers
Launch the browser visibly and allow manual CAPTCHA solving
ts
Copy
Edit
const browser = await chromium.launch({ headless: false });
🛠️ 4. Playwright Plugin: playwright-extra (with stealth)
bash
Copy
Edit
npm install playwright-extra playwright-extra-plugin-stealth
ts
Copy
Edit
const { chromium } = require('playwright-extra');
const stealth = require('playwright-extra-plugin-stealth')();
chromium.use(stealth);
const browser = await chromium.launch({ headless: false });
Helps bypass some bot detection mechanisms and reduce CAPTCHA appearance.
❌ 5. Not Recommended
Avoid attempts like image processing or machine learning to solve image CAPTCHAs directly—they're rarely reliable and often against TOS.
✅ Best Practices
Use stealth plugins
Avoid CAPTCHA triggers
Use legal CAPTCHA-solving APIs when needed
Prefer manual solving for one-off or low-scale tasks
Learn Playwright Training Course in Hyderabad
Read More
Authentication Testing with JWT in Playwright
Component Testing with Playwright
Accessibility Testing with Playwright
Multi-Tab Testing in Playwright
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment