Home > other >  is there any way to close popup while running headless chromium?
is there any way to close popup while running headless chromium?

Time:10-16

I am using the Playwright library to get data from a website. The issue is that website shows a popup when it open which results in stuck processes since the program is not able to get any button due to the popup.

Is there any way to close a popup each time it appears using the playwright?

CodePudding user response:

Closing actual popups

I think browser.once("targetcreated", ...) as described in this issue https://stackoverflow.com/a/57246544/2202112 could be something that you are looking for.

It allows you to setup a callback on newly created targets.

For reference, see puppeteer docs on "targetcreated"

Closing modals

  1. Find the selector for the button that dismisses the modal, for example .button-close or .button-dismiss.
  2. Use the page.click(selector, [options]) method to click the button before continuing further scraping

CodePudding user response:

As the documentation says Playwright will automatically dismiss all modal dialogs if there are no listeners of "dialog" event. Default behavior is essentially equivalent to

page.on('dialog', dialog => dialog.dismiss());

if it doesn't close in your case, it's most likely not a modal dialog (or a bug in playwright).

If the page opens another popup page, you can follow this guide but from your description it sounds more like a model dialog.

  • Related