5151)
5252from pydoll .protocol .base import EmptyResponse , Response
5353from pydoll .protocol .browser .events import (
54- BrowserEvent ,
5554 DownloadProgressEvent ,
5655 DownloadWillBeginEvent ,
5756)
@@ -851,9 +850,13 @@ async def expect_download(
851850 behavior = DownloadBehavior .ALLOW ,
852851 download_path = download_dir ,
853852 browser_context_id = self ._browser_context_id ,
854- events_enabled = True ,
855853 )
856854
855+ _page_events_was_enabled = True
856+ if not self ._page_events_enabled :
857+ _page_events_was_enabled = False
858+ await self .enable_page_events ()
859+
857860 loop = asyncio .get_event_loop ()
858861 will_begin : asyncio .Future [bool ] = loop .create_future ()
859862 done : asyncio .Future [bool ] = loop .create_future ()
@@ -889,13 +892,13 @@ async def on_progress(event: DownloadProgressEvent):
889892 if not done .done ():
890893 done .set_result (True )
891894
892- cb_id_will_begin = await self .on (
893- BrowserEvent .DOWNLOAD_WILL_BEGIN ,
895+ await self .on (
896+ PageEvent .DOWNLOAD_WILL_BEGIN ,
894897 cast (Callable [[dict ], Awaitable [Any ]], on_will_begin ),
895898 True ,
896899 )
897900 cb_id_progress = await self .on (
898- BrowserEvent .DOWNLOAD_PROGRESS ,
901+ PageEvent .DOWNLOAD_PROGRESS ,
899902 cast (Callable [[dict ], Awaitable [Any ]], on_progress ),
900903 False ,
901904 )
@@ -914,19 +917,37 @@ async def on_progress(event: DownloadProgressEvent):
914917 except asyncio .TimeoutError as exc :
915918 raise DownloadTimeout () from exc
916919 finally :
917- await self .remove_callback (cb_id_progress )
918- await self .remove_callback (cb_id_will_begin )
919- await self ._browser .set_download_behavior (
920- behavior = DownloadBehavior .DEFAULT ,
921- browser_context_id = self ._browser_context_id ,
920+ await self ._cleanup_download_context (
921+ cb_id_progress ,
922+ _page_events_was_enabled ,
923+ cleanup_dir ,
924+ state ,
925+ download_dir ,
922926 )
923927
924- if cleanup_dir :
925- file_path = state ['filePath' ]
926- if not file_path :
927- return
928- Path (file_path ).unlink (missing_ok = True )
929- shutil .rmtree (download_dir , ignore_errors = True )
928+ async def _cleanup_download_context (
929+ self ,
930+ cb_id_progress : int ,
931+ page_events_was_enabled : bool ,
932+ cleanup_dir : bool ,
933+ state : dict [str , Any ],
934+ download_dir : str ,
935+ ) -> None :
936+ await self .remove_callback (cb_id_progress )
937+ await self ._browser .set_download_behavior (
938+ behavior = DownloadBehavior .DEFAULT ,
939+ browser_context_id = self ._browser_context_id ,
940+ )
941+
942+ if cleanup_dir :
943+ file_path = state ['filePath' ]
944+ if not file_path :
945+ return
946+ Path (file_path ).unlink (missing_ok = True )
947+ shutil .rmtree (download_dir , ignore_errors = True )
948+
949+ if not page_events_was_enabled :
950+ await self .disable_page_events ()
930951
931952 @overload
932953 async def on (
0 commit comments