0

I'm creating a puppeteer script and occasionally an "Accept Cookies" window will pop up depending on which proxy I'm using and I have a function that will press the accept button when that window pops up, but occasionally the window won't pop up and I was wondering how would I write an if/else statement to have a handling for whether the window will be visible or not. For example if cookies is visible => clicks accept | else if cookie isn't visible => ignore. I had one written up as if (cookies == true) => function | else if (cookies == null) => move on, but it didn't work as I thought. Does anyone have any ideas?

const acceptCookies = `button[class="ncss-btn-primary-dark pt3-sm pb3-sm pt2-lg pb2-lg ta-sm-c u-full-width"]`

    await page.waitForSelector(acceptCookies, { visible: true });
        await Promise.all([
            page.click(acceptCookies),
            page.waitForNavigation()
        ]);

    await page.waitForTimeout( 2500 );
JediSZN
  • 59
  • 6
  • https://stackoverflow.com/questions/19669786/check-if-element-is-visible-in-dom or https://stackoverflow.com/questions/1343237/how-to-check-elements-visibility-via-javascript – WOUNDEDStevenJones Jan 06 '23 at 14:48
  • [this](https://stackoverflow.com/questions/58675083/how-can-i-check-if-selector-exists-in-puppeteer) or [that](https://stackoverflow.com/a/57471782/4935162) – Yarin_007 Jan 06 '23 at 14:49
  • i wrote it as this, would this be correct? https://codeshare.io/wnR7BP – JediSZN Jan 06 '23 at 15:07
  • Which site? I'd install a MutationObserver that watches for the element popping up, then clicks on the close button. If you follow the ideas above, they block the main thread and set timeouts rather than run in the background. [Avoid `await page.waitForTimeout( 2500 );`](https://stackoverflow.com/questions/46919013/puppeteer-wait-n-seconds-before-continuing-to-the-next-line/73676564#73676564) except for temporary debugging. – ggorlen Jan 06 '23 at 18:08
  • The site is nike.com, it seems that certain elements go invisible when running my puppeteer script – JediSZN Jan 06 '23 at 18:35

0 Answers0