Puppeteer 没有点击带有文本的按钮
Puppeteer not clicking button with text
我有一个尝试接受 cookie 的简单函数
这是我的代码:
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://www.sport1.de/live/darts-sport');
await page.click('button[text=AKZEPTIEREN]');
// await page.screenshot({ path: 'example.png' });
// await browser.close();
})();
cookie 弹出窗口放置在 iframe 中。您必须通过 contentFrame 切换到 iframe 才能点击接受按钮。
另外,如果你想通过textContent过滤,你需要使用XPath。使用 CSS 选择器,您无法通过其 textContent 获取元素。
const cookiePopUpIframeElement=await page.$("iframe[id='sp_message_iframe_373079']");
const cookiePopUpIframe=await cookiePopUpIframeElement.contentFrame();
const acceptElementToClick = await cookiePopUpIframe.$x("//button[text()='AKZEPTIEREN']");
await acceptElementToClick[0].click();
我有一个尝试接受 cookie 的简单函数
这是我的代码:
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://www.sport1.de/live/darts-sport');
await page.click('button[text=AKZEPTIEREN]');
// await page.screenshot({ path: 'example.png' });
// await browser.close();
})();
cookie 弹出窗口放置在 iframe 中。您必须通过 contentFrame 切换到 iframe 才能点击接受按钮。
另外,如果你想通过textContent过滤,你需要使用XPath。使用 CSS 选择器,您无法通过其 textContent 获取元素。
const cookiePopUpIframeElement=await page.$("iframe[id='sp_message_iframe_373079']");
const cookiePopUpIframe=await cookiePopUpIframeElement.contentFrame();
const acceptElementToClick = await cookiePopUpIframe.$x("//button[text()='AKZEPTIEREN']");
await acceptElementToClick[0].click();