Home > Blockchain >  Puppeteer not clicking button with text
Puppeteer not clicking button with text

Time:11-16

I have a simple function that tries to accept the cookies
Here's my code:

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

enter image description here

CodePudding user response:

The cookie popup is placed in an iframe. You have to switch to iframe by contentFrame to be able to click on the accept button.

Also, if you want to filter by textContent, you need to use XPath. With CSS selector you can't get elements by its 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();
  • Related