0

I am struggling with a cookie consent layer within a shadowroot which i currently wait for by checking via window.view if it has appeared and then click the button via page.evaluate(()=>document.querySelector I would much rather wait for it to appear and click it.

However, i dont seem to correctly wait for it and my google searches didnt show up an example either. The API documentation on github didnt provide examples for my case either.

these wait tries didnt work, meaning it did not detect the cookie conest layer:

      await page.waitForFunction(
        'page.evaluate(()=>document.querySelector("#page_reg > div:nth-child(1)").shadowRoot.querySelector("#consent-layer > div.consent-layer__btn-container > button.btn.btn--secondary.js-accept-essential-cookies"))',
       );
      await page.waitForFunction(
        'document.querySelector("#page_reg > div:nth-child(1)").shadowRoot.querySelector("#consent-layer > div.consent-layer__btn-container > button.btn.btn--secondary.js-accept-essential-cookies")',
       );

if i remove the '' (which i DIDNT find in any examples on the web)

      await page.waitForFunction(
        document.querySelector("#page_reg > div:nth-child(1)").shadowRoot.querySelector("#consent-layer > div.consent-layer__btn-container > button.btn.btn--secondary.js-accept-essential-cookies"),
       );

i get the following error:


            throw new Error('Evaluation failed: ' + (0, util_js_1.getExceptionMessage)(exceptionDetails));
              ^

Error: Evaluation failed: TypeError: null is not an object (evaluating 'null.querySelector')

this does correctly click my button for accepting cookies (without waitForFunction!, and only if the button is already present):

  await page.evaluate(()=>document.querySelector("#page_reg > div:nth-child(1)").shadowRoot.querySelector("#consent-layer > div.consent-layer__btn-container > button.btn.btn--secondary.js-accept-essential-cookies").click())

I tried looking for the selector in different ways but it did not find it. I want to wait for it to appear, then proceeed with my script. It will take a few seconds to appear, but sometimes longer, so i dont want to use a simple 5sec wait.

  • Putting `page.evaluate` in a `waitForFunction` won't work--that's Node/Puppeteer code you're trying to run in the browser console. Same with the version that tries to run `querySelector` (a browser DOM method) in Node. Just take your last example and swap `evaluate` to `waitForFunction`. If that doesn't work, please share a [mcve] with the site itself. – ggorlen Nov 18 '22 at 13:30
  • unfortunately this didnt work. unfortunately i cannot share a minimal reproducible example due to the fact that the specific site is behind paywall. i tried await page.waitForFunction(()=>document.querySelector("#page_reg > div:nth-child(1)").shadowRoot.querySelector("#consent-layer > div.consent-layer__btn-container > button.btn.btn--secondary.js-accept-essential-cookies")) like you said but it timed out after 30secs even though the consent layer appeared in time – lealea93592 Nov 19 '22 at 18:35
  • hi. as stated in this line of code which clicks the element which i want to wait for: await page.evaluate(()=>document.querySelector("#page_reg > div:nth-child(1)").shadowRoot.querySelector("#consent-layer > div.consent-layer__btn-container > button.btn.btn--secondary.js-accept-essential-cookies").click()) the element itself is in a shadowroot. unfortunately waitForSelector didnt seem to work on those shadowroots thats why i tried waitforfunction. again, unfortunately the site i am testing on is not publicly available :( – lealea93592 Nov 20 '22 at 08:46
  • Please make a reproducible example of that root and inject the relevant HTML. Check out [Puppeteer not giving accurate HTML code for page with shadow roots](https://stackoverflow.com/questions/68525115/puppeteer-not-giving-accurate-html-code-for-page-with-shadow-roots/68540701#68540701) where I list about a dozen shadow root resources. That said, your general approach seems OK, so I'm not sure what the problem could be without seeing the site we're working with. – ggorlen Nov 20 '22 at 16:15

0 Answers0