Short Answer
const element = await page.waitForSelector('a.cookie-close', { visible: true });
await element.click();
This uses the page.waitForSelector
function to select a visible element with the selector a.cookie-close
. After the selector is queried, the code uses elementHandle.click
to click on it.
Explanation
Only the functions page.waitForSelector
and page.waitForXPath
have an option built in that checks if an element is not only present but also visible. When used, puppeteer will check if the style attribute visibility
is not hidden
and if the element has a visible bounding box.
Making sure the element is not empty
Even if the element is visible, it might be empty (e.g. <span></span>
). If you also want the element not to be empty too, you can use the following query instead:
const element = await page.waitForSelector('SELECTOR:not(:empty)', { visible: true });
This will in addition use the pseudo selectors :empty
and :not
to make sure that the element contains a child node or text. If you want to query for a specific text inside the element, you might want to check out this answer.