【问题标题】:Puppeteer not clicking button with textPuppeteer 没有点击带有文本的按钮
【发布时间】:2021-11-15 14:50:03
【问题描述】:

我有一个尝试接受 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();
    })();

【问题讨论】:

    标签: node.js puppeteer


    【解决方案1】:

    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();
    

    【讨论】:

    • 谢谢,但在最后一行你必须添加 [0].click();
    • 谢谢,已更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多