python selenium,找到下载完成后?

app*_*der 13 python selenium

我用selenium开始下载.下载完成后,需要采取某些措施,是否有任何简单的方法可以找出下载完成的时间?(我正在使用FireFox驱动程序)

ale*_*cxe 16

没有内置的selenium方式等待下载完成.


这里的一般想法是等到文件出现在"下载"目录中.

这可能是通过一遍又一遍地检查文件是否存在来实现的:

或者,通过使用watchdog监视目录之类的东西:


Aus*_*lop 8

我最近遇到了这个问题。我一次下载了多个文件,如果下载失败,我必须以一种超时的方式进行构建。

该代码每秒检查一些下载目录中的文件名,并在文件名完成或退出时间超过20秒后退出。返回的下载时间用于检查下载是否成功或是否超时。

import time
import os

def download_wait(path_to_downloads):
    seconds = 0
    dl_wait = True
    while dl_wait and seconds < 20:
        time.sleep(1)
        dl_wait = False
        for fname in os.listdir(path_to_downloads):
            if fname.endswith('.crdownload'):
                dl_wait = True
        seconds += 1
    return seconds
Run Code Online (Sandbox Code Playgroud)

我相信这仅适用于chrome文件,因为它们以.crdownload扩展名结尾。可能有类似的方法可以签入其他浏览器。

编辑:最近,我更改了.crdownload在没有显示为扩展名的时间使用此功能的方式。本质上,这也只是等待正确数量的文件。

def download_wait(directory, timeout, nfiles=None):
    """
    Wait for downloads to finish with a specified timeout.

    Args
    ----
    directory : str
        The path to the folder where the files will be downloaded.
    timeout : int
        How many seconds to wait until timing out.
    nfiles : int, defaults to None
        If provided, also wait for the expected number of files.

    """
    seconds = 0
    dl_wait = True
    while dl_wait and seconds < timeout:
        time.sleep(1)
        dl_wait = False
        files = os.listdir(directory)
        if nfiles and len(files) != nfiles:
            dl_wait = True

        for fname in files:
            if fname.endswith('.crdownload'):
                dl_wait = True

        seconds += 1
    return seconds
Run Code Online (Sandbox Code Playgroud)


小智 8

import os
import time

def latest_download_file():
      path = r'Downloads folder file path'
      os.chdir(path)
      files = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
      newest = files[-1]

      return newest

fileends = "crdownload"
while "crdownload" == fileends:
    time.sleep(1)
    newest_file = latest_download_file()
    if "crdownload" in newest_file:
        fileends = "crdownload"
    else:
        fileends = "none"
Run Code Online (Sandbox Code Playgroud)

这是几个解决方案的组合。我不喜欢我必须扫描整个下载文件夹才能找到以“crdownload”结尾的文件。此代码实现了一个功能,该功能可在下载文件夹中提取最新文件。然后它只是检查该文件是否仍在下载。将它用于我正在构建的 Selenium 工具,效果很好。


C. *_*eng 6

正如之前所回答的,没有本地方法可以检查下载是否完成。因此,这里有一个辅助函数,可以为 Firefox 和 Chrome 完成这项工作。一个技巧是在开始新下载之前清除临时下载文件夹。另外,使用本机 pathlib 进行跨平台使用。

from pathlib import Path

def is_download_finished(temp_folder):
    firefox_temp_file = sorted(Path(temp_folder).glob('*.part'))
    chrome_temp_file = sorted(Path(temp_folder).glob('*.crdownload'))
    downloaded_files = sorted(Path(temp_folder).glob('*.*'))
    if (len(firefox_temp_file) == 0) and \
       (len(chrome_temp_file) == 0) and \
       (len(downloaded_files) >= 1):
        return True
    else:
        return False
Run Code Online (Sandbox Code Playgroud)


DHS*_*DHS 6

我知道答案为时已晚,但我想为未来的读者分享一个黑客。

您可以从主线程创建一个线程,比如thread1并在此处启动下载。现在,创建另一个线程,比如thread2并在那里,让它使用 join() 方法等待thread1完成。现在在这里,您可以在下载完成后继续执行流程。

仍然确保您不要使用 selenium 启动下载,而是使用 selenium 提取链接并使用请求模块进行下载。

使用请求模块下载

例如:

def downloadit():
     #download code here    

def after_dwn():
     dwn_thread.join()           #waits till thread1 has completed executing
     #next chunk of code after download, goes here

dwn_thread = threading.Thread(target=downloadit)
dwn_thread.start()

metadata_thread = threading.Thread(target=after_dwn)
metadata_thread.start()
Run Code Online (Sandbox Code Playgroud)