Selenium (Python) - waiting for a download process to complete using Chrome web driver

后端 未结 9 1173

I\'m using selenium and python via chromewebdriver (windows) in order to automate a task of downloading large amount of files from different pages. My code works, but the so

9条回答
  •  情歌与酒
    2020-12-04 19:07

    I have had the same problem and found a solution. You can check weither or not a .crdownload is in your download folder. If there are 0 instances of a file with .crdownload extension in the download folder then all your downloads are completed. This only works for chrome and chromium i think.

    def downloads_done():
        while True:
            for filename in os.listdir("/downloads"):
                if ".crdownload" in i:
                    time.sleep(0.5)
                    downloads_done()
    

    Whenever you call downloads_done() it will loop itself untill all downloads are completed. If you are downloading massive files like 80 gigabytes then i don't recommend this because then the function can reach maximum recursion depth.

    2020 edit:

    def wait_for_downloads():
        print("Waiting for downloads", end="")
        while any([filename.endswith(".crdownload") for filename in 
                   os.listdir("/downloads")]):
            time.sleep(2)
            print(".", end="")
        print("done!")
    

    The "end" keyword argument in print() usually holds a newline but we replace it. While there are no filenames in the /downloads folder that end with .crdownload sleep for 2 seconds and print one dot without newline to console

    I don't really recommend using selenium anymore after finding out about requests but if it's a very heavily guarded site with cloudflare and captchas etc then you might have to resort to selenium.

提交回复
热议问题