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

后端 未结 9 1182

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:14

    I had the same problem and this method worked for me.

    import time
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.common.exceptions import ElementClickInterceptedException
    from threading import Thread
    import os
    import datetime
    def checkFilePresence(downloadPath, numberOfFilesInitially, artistName, 
        songTitle):
        timeNow = datetime.datetime.now()
        found = False
        while not found:
            numberOfFilesNow = len(os.listdir(downloadPath))
            if numberOfFilesNow > numberOfFilesInitially:
                for folders, subfolders, files in os.walk(downloadPath):
                    for file in files:
                        modificationTime = datetime.datetime.fromtimestamp\
                        (os.path.getctime(os.path.join(folders, file)))
                        if modificationTime > timeNow:
                            if file.endswith('.mp3'):
                                return
    

提交回复
热议问题