I’m in the process of extracting data from a website where I occasionally encounter reCAPTCHA v2 challenges. To tackle these challenges, I utilize the 2Captcha service, and once I get the API response (a string), I need to place this response into the reCAPTCHA response field located in an iframe: textarea[name="g-recaptcha-response"]
. The issue arises because the desired input field is nested within an iframe, making it challenging to use page.evaluate
directly in Playwright. Below is my attempt at the solution:
let responseKey: string;
await captchaSolver
.recaptcha({
pageUrl: page.url(),
captchaKey: src,
isInvisible: true,
})
.then(async (result) => {
responseKey = result.data;
const iframeLocator = page.frameLocator('div iframe[title="reCAPTCHA"]');
const iframe = iframeLocator.first();
const allFrames = await page.frames();
const reCaptchaFrame = allFrames[1];
await reCaptchaFrame.evaluate(() => {
const inputField = document.querySelector(
'textarea[name="g-recaptcha-response"]'
) as HTMLTextAreaElement;
inputField.value = responseKey;
}, responseKey);
await iframe.locator('#recaptcha-anchor > div.recaptcha-checkbox-border').click();
})
.catch((error) => {
console.error(error);
});
Unfortunately, I encountered an error stating: “Property ‘evaluate’ does not exist in type ‘FrameLocator’.ts(2339)”.