Open abhash-rai opened 6 days ago
You need handlers for both cdp.page. DownloadWillBegin and DownloadProgress
Thanks this worked:
import asyncio
import nodriver as uc
from nodriver import cdp
binded_tabs = []
async def bind_handlers(browser):
global binded_tabs
while True:
await asyncio.sleep(0.01)
for tab in browser.tabs:
if tab not in binded_tabs:
tab.add_handler(cdp.page.DownloadWillBegin, lambda event: print('Download event => %s' % event.guid))
binded_tabs.append(tab)
async def crawl():
browser = await uc.start(headless=False)
asyncio.create_task(bind_handlers(browser))
await browser.get("https://www.python.org/ftp/python/3.13.0/python-3.13.0-amd64.exe")
await browser.get("https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-user", new_tab=True)
while True:
await asyncio.sleep(0.2) # Keep the event loop alive
if __name__ == '__main__':
uc.loop().run_until_complete(crawl())
However sometimes when clicking a download button it redirects to a new tab entirely from where download will begin. In this case it doesn't detect download.
I want to be able to add handlers to every opened tab current or future. How can I do this? Is there a cdp event for this as well? I checked out cdp.browser and it has DownloadWillBegin event class but when I use cdp.browser.DownloadWillBegin to above code the function to be called on download start event which in this case is a basic lambda logging function is not called.
My aim is to detect download at browser level across every tab currently opened or future tabs.
Hello. I just wanted to make a nodriver script that can detect downloads and its status. I tried to print logging "Download Started..." whenever tab download begins but it doesn't print the log which leads me to believe the event in my script is not setup correctly. I would also be grateful if anyone can add the functionality to detect download completion if download was started. Thanks in advance!