Handling Errors and CAPTCHAs
Playwright is a powerful tool for automating browser interactions, but like any tool, it’s not immune to errors or challenges such as CAPTCHAs.
In this chapter, we’ll explore how to handle errors and CAPTCHAs when using Playwright.
1. Handling Errors
Errors can occur at any point during the execution of your script. Handling these errors is important so your script can recover or exit cleanly.
Here’s an example of how you might handle errors in Playwright:
const playwright = require('playwright');
(async () => {
try {
const browser = await playwright['chromium'].launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://cnn.com');
// We can perform our task here
await browser.close();
} catch (error) {
console.error('An error occurred:', error);
}
})();
We can also modify the script to retry the operation a specified number of times. If the operation fails repeatedly, the script will log an error message and terminate.
const playwright = require('playwright');
const MAX_RETRIES = 5; // Maximum number of retries
async function run() {
let retries = 0;
while (retries < MAX_RETRIES) {
try {
const browser = await playwright['chromium'].launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://cnn.com');
// Perform actions...
await browser.close();
// If the actions are performed successfully, break the loop
break;
} catch (error) {
console.error('An error occurred:', error);
console.log('Retrying...');
retries++;
if (retries === MAX_RETRIES) {
console.log('Maximum retries reached. Exiting.');
break;
}
}
}
}
run();
2. Handling CAPTCHAs
CAPTCHAs are designed to prevent automated interactions, so they can pose a significant challenge to a Playwright script.
There are several strategies you can use to handle CAPTCHAs:
- Avoidance: Some sites may present a CAPTCHA challenge after a certain number of actions have been performed in a short period of time. By introducing the different methods that we discussed above in your Playwright script, you may be able to avoid triggering the CAPTCHA.
- Manual Intervention: If a CAPTCHA does appear, you could pause the execution of your script and wait for a human to solve it manually.
- Third-Party Services: Several third-party services, such as 2Captcha and Anti-Captcha provide APIs to solve CAPTCHAs. These services generally work by sending the CAPTCHA image to a server, where a human or advanced OCR software solves it and sends back the solution.
Here’s an example of how you might use such a service in your Playwright script
const playwright = require('playwright');
const axios = require('axios'); // for making HTTP requests
// Replace with your 2Captcha API key
const captchaApiKey = 'YOUR_2CAPTCHA_API_KEY';
async function solveCaptcha(captchaImage) {
// Send a request to the 2Captcha service
const response = await axios.post('https://2captcha.com/in.php', {
method: 'base64',
key: captchaApiKey,
body: captchaImage,
});
if (response.data.status !== 1) {
throw new Error('Failed to submit CAPTCHA for solving');
}
// Poll the 2Captcha service for the solution
while (true) {
const solutionResponse = await axios.get(
`https://2captcha.com/res.php?key=${captchaApiKey}&action=get&id=${response.data.request}`
);
if (solutionResponse.data.status === 1) {
return solutionResponse.data.request;
}
// If the CAPTCHA is not solved yet, wait for a few seconds before polling again
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
(async () => {
const browser = await playwright['chromium'].launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://example.com');
// If a CAPTCHA appears...
const captchaElement = await page.$('#captcha');
if (captchaElement) {
// Take a screenshot of the CAPTCHA image
const captchaImage = await captchaElement.screenshot({ encoding: 'base64' });
// Solve the CAPTCHA
const captchaSolution = await solveCaptcha(captchaImage);
// Enter the CAPTCHA solution
await page.fill('#captchaSolution', captchaSolution);
}
await browser.close();
})();
In this example, when a CAPTCHA is detected, the script takes a screenshot of the CAPTCHA image and sends it to the 2Captcha service for solving.
It then polls the 2Captcha service for the solution and enters it on the page. Please be aware that this solution depends highly on the website you are trying to access.