robinnlmn
robinnlmn

Reputation: 31

Puppeteer not clicking button with text

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

Upvotes: 0

Views: 719

Answers (1)

Y sharifpour
Y sharifpour

Reputation: 389

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

Upvotes: 1

Related Questions