python selenium Chrome 监测文件是否下载完毕

一、版本

python 3.5

selenium 4.1.0

pycharm 社区版

二、代码

其实我感觉firefox的也可以。这个方法网上是没有的,selenium也未提供下载完成判断。


    @staticmethod
    def judge_file_exist(is_check,check_times,check_interval_sec,check_path,filter_down_file,check_ext):
        """
        检测函数
        :param is_check:是否检测True表示检测False 不检查
        :param check_times:检测次数
        :param check_interval:检测时间间隔(默认值)
        :param check_path:检测路径(默认值)
        :param chedk_ext:检测扩展名(默认值)
        :return:返回真假
        """
        if os.path.exists(check_path) is False:
            raise Exception("路径不存在...")
        if str.isdigit(str(check_times)) is False:
            raise Exception("检测次数不是数字...")
        if is_check is False:
            return True,"不进行检测" #直接执行下一步操作
        else:
            for number in range(1,int(check_times)):
                print("正在进行第" str(number) "次检测...")
                sleep(0.5) #延迟1s经常检测不到
                files = os.listdir(check_path) # 读取目录下所有文件
                if len(filter_down_file) > 0:
                    for file in files:
                        for filter_file in filter_down_file:
                            if str(file) in str(filter_file):
                                files.remove(filter_file)
                                BaseFrame().right(filter_file   "文件被过滤...")
                                pass

                file_number=len(files)
                if file_number ==0:
                    #不存在任何文件,休眠一会 继续执行下一次
                    sleep(int(check_interval_sec))  # 休眠一会
                    return False, "0个文件认定是False"
                elif file_number==1:
                    file_name=files[0]
                    #crdownload
                    file_full_name=check_path os.sep file_name
                    file_ext=os.path.splitext(file_full_name)[-1]
                    if "crdownload"==str(file_ext.split(".")[1]):
                        sleep(int(check_interval_sec))  # 休眠一会
                        continue
                    if "tmp" == str(file_ext.split(".")[1]):
                        sleep(int(check_interval_sec))  # 休眠一会
                        continue
                    if "exe" == str(file_ext.split(".")[1]):
                        os.remove(file_full_name)
                        print("清理exe文件")
                        continue
                    for e in check_ext.split("|"):
                        if e == str(file_ext.split(".")[1]).lower():
                            return True,file_full_name
                        else:
                            sleep(int(check_interval_sec))  # 休眠一会
                            continue
                else:
                    return False,"多个文件认定是False"
            return False,"可能是不存在文件或者是tmp或crdownload文件"

三、核心

核心就是定期检测指定文件夹。 监测过程有很多特例需要判断。

一、版本

python 3.5

selenium 4.1.0

pycharm 社区版

二、代码

其实我感觉firefox的也可以。这个方法网上是没有的,selenium也未提供下载完成判断。


    @staticmethod
    def judge_file_exist(is_check,check_times,check_interval_sec,check_path,filter_down_file,check_ext):
        """
        检测函数
        :param is_check:是否检测True表示检测False 不检查
        :param check_times:检测次数
        :param check_interval:检测时间间隔(默认值)
        :param check_path:检测路径(默认值)
        :param chedk_ext:检测扩展名(默认值)
        :return:返回真假
        """
        if os.path.exists(check_path) is False:
            raise Exception("路径不存在...")
        if str.isdigit(str(check_times)) is False:
            raise Exception("检测次数不是数字...")
        if is_check is False:
            return True,"不进行检测" #直接执行下一步操作
        else:
            for number in range(1,int(check_times)):
                print("正在进行第" str(number) "次检测...")
                sleep(0.5) #延迟1s经常检测不到
                files = os.listdir(check_path) # 读取目录下所有文件
                if len(filter_down_file) > 0:
                    for file in files:
                        for filter_file in filter_down_file:
                            if str(file) in str(filter_file):
                                files.remove(filter_file)
                                BaseFrame().right(filter_file   "文件被过滤...")
                                pass

                file_number=len(files)
                if file_number ==0:
                    #不存在任何文件,休眠一会 继续执行下一次
                    sleep(int(check_interval_sec))  # 休眠一会
                    return False, "0个文件认定是False"
                elif file_number==1:
                    file_name=files[0]
                    #crdownload
                    file_full_name=check_path os.sep file_name
                    file_ext=os.path.splitext(file_full_name)[-1]
                    if "crdownload"==str(file_ext.split(".")[1]):
                        sleep(int(check_interval_sec))  # 休眠一会
                        continue
                    if "tmp" == str(file_ext.split(".")[1]):
                        sleep(int(check_interval_sec))  # 休眠一会
                        continue
                    if "exe" == str(file_ext.split(".")[1]):
                        os.remove(file_full_name)
                        print("清理exe文件")
                        continue
                    for e in check_ext.split("|"):
                        if e == str(file_ext.split(".")[1]).lower():
                            return True,file_full_name
                        else:
                            sleep(int(check_interval_sec))  # 休眠一会
                            continue
                else:
                    return False,"多个文件认定是False"
            return False,"可能是不存在文件或者是tmp或crdownload文件"

三、核心

核心就是定期检测指定文件夹。 监测过程有很多特例需要判断。

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在