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.