diff --git a/npiai/core/browser/_playwright.py b/npiai/core/browser/_playwright.py index bd5bc6b2..c2bb82e8 100644 --- a/npiai/core/browser/_playwright.py +++ b/npiai/core/browser/_playwright.py @@ -12,6 +12,8 @@ Page, FileChooser, StorageState, + Dialog, + Download, ) __BROWSER_UTILS_VERSION__ = "0.0.15" @@ -114,11 +116,32 @@ def block_route(route): ) self.page = await self.context.new_page() + self.page.on("dialog", self.on_dialog) + self.page.on("download", self.on_download) self.page.on("filechooser", self.on_filechooser) self.page.on("popup", self.on_popup) + self.context.on("close", self.on_close) self.ready = True + async def on_dialog(self, dialog: Dialog): + """ + Callback function invoked when a dialog is opened + + Args: + dialog: Dialog instance + """ + await dialog.dismiss() + + async def on_download(self, download: Download): + """ + Callback function invoked when a download is started + + Args: + download: Download instance + """ + await download.cancel() + async def on_filechooser(self, chooser: FileChooser): """ Callback function invoked when an input:file is clicked @@ -138,6 +161,12 @@ async def on_popup(self, popup: Page): print(f"popup {popup}") self.page = popup + async def on_close(self, ctx: BrowserContext): + """ + Callback function invoked when the page is closed + """ + self.page = ctx.pages[-1] if ctx.pages else None + async def stop(self): """ Dispose the chrome tools