【问题标题】:Wait till download is complete in Python+Selenium+Firefox在 Python+Selenium+Firefox 中等待下载完成
【发布时间】:2018-05-25 08:43:32
【问题描述】:

我想在文件下载完成后立即关闭浏览器。我有以下代码,但它没有关闭浏览器。我一定是哪里错了。请帮帮我。

driver.find_element_by_link_text("[Comma-Delimited Text (CSV)]").click()
while True:
    if os.path.isfile('C:\\Python34\\*.part'):
        time.sleep(10)
    elif os.path.isfile('C:\\Python34\\*.csv'):
        break
    else:
        time.sleep(10)


def tearDown(self):
    self.driver.quit()
    self.assertEqual([], self.verificationErrors)

【问题讨论】:

    标签: python python-3.x selenium firefox


    【解决方案1】:

    os.path.isfile() 不支持 glob 样式的路径定义导致循环永远不会退出。

    您需要 glob.glob()fnmatch 代替:

    您还可以使用 watchdog 之类的模块来监控目录中的更改:

    【讨论】:

    • 正是我想要的,当文件下载时间介于几秒到几分钟之间时,它非常有用。非常感谢!
    【解决方案2】:

    我写了一个zip文件下载的下载方法。这可能会有所帮助。单击下载按钮后,将调用此方法并等待下载完成

    def downloader() :
        print("File Download Started.......")
        sleep(60)
        while True:
            file = glob.glob(BASE_FILE_DIR+'*.zip.part')
            if file:
                print("Download Pending.......")
                sleep(60)
                continue
            else:
                print('File Downloaded')
                break
    

    【讨论】:

      猜你喜欢
      • 2023-01-28
      • 1970-01-01
      • 2015-03-14
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 2022-07-27
      • 1970-01-01
      相关资源
      最近更新 更多