我正在使用"puppeteer": "^19.11.1",
:
我创建了这个功能,按下这个Page上的同意按钮:
这就是我的功能:
async function handleConsent(page, logger) {
const consentButtonSelector =
'#uc-center-container > div.sc-eBMEME.ixkACg > div > div > div > button.sc-dcJsrY.bSKKNx';
try {
// Wait for the iframe to load
await page.waitForSelector("iframe", { timeout: 3000 });
// Identify the iframe that contains the consent button
const iframeElement = await page.$(
'iframe[name="__tcfapiLocator"]'
);
if (iframeElement) {
const iframeContent = await iframeElement.contentFrame();
// Attempt to click the consent button within the iframe
const consentButton = await iframeContent.$(consentButtonSelector);
if (consentButton) {
await iframeContent.click(consentButtonSelector);
logger.info("Consent button clicked inside the iframe.");
} else {
logger.info("Consent button not found inside the iframe.");
}
} else {
logger.info("Iframe with the consent message not found.");
}
await page.waitForTimeout(3000); // Wait for any potential redirects or updates after clicking
} catch (error) {
logger.error(`An error occurred while handling consent: ${error}`);
}
}
我的问题是没有找到 Select 器,即使我试图 Select iframe.
有什么建议我做错了吗?
感谢您的回复!