【问题标题】:How to get last downloaded file using selenium如何使用硒获取最后下载的文件
【发布时间】:2015-09-08 09:50:55
【问题描述】:

无论如何我可以使用 selenium 获取最后下载的文件。目前我正在使用 selenium 下载 Excel 文件,我需要获取该文件并阅读它。阅读部分已涵盖,但我需要下载的文件路径和文件名才能阅读它。到目前为止,我还没有找到任何可以提供帮助的东西。我主要在寻找 google chrome 解决方案,但 firefox 也可以。

提前致谢

【问题讨论】:

    标签: file selenium automation


    【解决方案1】:
    import os
    import glob
    
    home = os.path.expanduser("~")
    downloadspath=os.path.join(home, "Downloads")
    list_of_files = glob.glob(downloadspath+"\*.pptx") # * means all if need specific format then *.csv
    latest_file = max(list_of_files, key=os.path.getctime)
    

    获取下载文件夹中最后一个文件的路径的简化解决方案。上面的代码将获取Downlodas 中最新的.pptx 文件的路径。根据需要更改扩展名。或者你可以选择不指定扩展名

    【讨论】:

      【解决方案2】:

      下面是可以帮助解决上述查询的代码 sn-p:

      **Changes in driver file:**
      
      protected File downloadsDir = new File("");
      if (browser.equalsIgnoreCase("firefox")) 
      {
          downloadsDir = new File(System.getProperty("user.dir") + File.separatorChar + "downloads");
          if (!downloadsDir.exists()) 
           {
              boolean ddCreated = downloadsDir.mkdir();
              if (!ddCreated) {
                  System.exit(1);
              }
          }
      }
      
      
      /*Firefox browser profile*/
      FirefoxProfile firefoxProfile = new FirefoxProfile();
      firefoxProfile.setPreference("browser.download.folderList", 2);
      firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
      firefoxProfile.setPreference("browser.download.dir", downloadsDir.getAbsolutePath());
      firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/plain,application/octet-stream");
      
      **Empty the download directory[Can be implemented as @BeforeClass]:**
      
      public void emptyDownloadsDir()
      {
          // Verify downloads dir is empty, if not remove all files.
          File[] downloadDirFiles = downloadsDir.listFiles();
          if (downloadDirFiles != null) {
              for (File f : downloadDirFiles) {
                  if (f.exists()) {
                      boolean deleted = FileUtility.delete(f);
                      assertTrue(deleted, "Files are not deleted from system local directory" + downloadsDir + ", skipping the download tests.");
                  }
              }
          }
      }
      
      
      **Check the Latest downloaded file:**
      
      /*Test file*/
      protected static String EXCEL_FILE_NAME= Test_Excel_File.xls;
      
      protected static int WAIT_IN_SECONDS_DOWNLOAD = 60;
      
      // Wait for File download.
      int counter = 0;
      while (counter++ < WAIT_IN_SECONDS_DOWNLOAD && (downloadsDir.listFiles().length != 1 || downloadsDir.listFiles()[0].getName().matches(EXCEL_FILE_NAME))) {
          this.wait(2);
      }
      
      // Verify the downloaded File by comparing name.
      File[] downloadDirFiles = downloadsDir.listFiles();
      String actualName = null;
      for (File file : downloadDirFiles) {
          actualName = file.getName();
          if (actualName.equals(EXCEL_FILE_NAME)) {
              break;
          }
      }
      assertEquals(actualName, EXCEL_FILE_NAME, "Last Downloaded File name does not matches.");
      

      【讨论】:

      • System.exit(1) 不好,因为它将结束当前的 JVM。你可能不想要那个。
      【解决方案3】:

      您可以使用配置文件将下载保存到修复位置。查看这些讨论:

      Downloading file to specified location with Selenium and python
      Access to file download dialog in Firefox

      正如您所提到的,您已经涵盖了阅读部分。您可以从该固定位置读取它。

      【讨论】:

        猜你喜欢
        • 2022-01-01
        • 2013-09-28
        • 1970-01-01
        • 1970-01-01
        • 2011-07-08
        • 2021-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多