0

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?

Edit:

Page is creating a pop-up. I inspect the page and learnt that they are not using the modal for the pop instead they wrote the inline css and decorated a div as the popup.

bSr
  • 1,410
  • 3
  • 16
  • 30

2 Answers2

1

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
Eelke van den Bos
  • 1,423
  • 1
  • 13
  • 18
1

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 modal dialog.

Yury Semikhatsky
  • 2,144
  • 13
  • 12
  • You are right, but somehow the popup is not closed by default in my case, any idea? – bSr Oct 19 '21 at 12:19
  • 1
    It might be a bug, you can file an issue at https://github.com/microsoft/playwright/issues with a repro so that we can have a look. – Yury Semikhatsky Oct 19 '21 at 17:44