如何获取正在下载的URL或文件名? [英] How to get the url or filename being downloaded now?

查看:260
本文介绍了如何获取正在下载的URL或文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击按钮后,如何知道下载的URL(带有文件名),或者

After click the button, how to know the downloading url(with the filename), or

如何知道正在下载的文件名(带有扩展名)?一个问题是例如下载的文件有些扩展名为.csv,有些则没有.

how to know the filename(complete with extension) being downloaded? One problem is, e.g. the downloaded file some have .csv extension, some without.

例如我想统一重命名. (请不要去D/L DIR,找到文件并重命名)

e.g. I would like to rename it unified. (pls. don't wanna go to the D/L DIR, find the file and rename it)

from selenium import webdriver
from selenium.webdriver.firefox.options import Options 
...
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')
...
driver = webdriver.Firefox(profile, options=opts, executable_path=FIREFOX_GOCKO_DRIVER_PATH)

driver.get(url)
driver.find_element_by_id(Button).click()

print("The file being downloaded is... ", ??? )
print("File is being downloaded from...", ?url?)

推荐答案

这是获取最新下载的文件名和URL的简单解决方案.

Here is the simple solution to get the latest downloaded file name and url.

注意:在运行下面的代码之前,请考虑文件下载已完成.

如果您要脚本等到下载完成,请在答案末尾检查 getDownLoadedFileName 方法.

If you want the script wait until the download completed, then check the getDownLoadedFileName method at the end of the answer.

# open a new tab
driver.execute_script("window.open()")
# switch to new tab
driver.switch_to.window(driver.window_handles[-1])
# navigate to chrome downloads
driver.get('chrome://downloads')
# get the latest downloaded file name
fileName = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content  #file-link').text")
# get the latest downloaded file url
sourceURL = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content  #file-link').href")
# print the details
print(fileName)
print (sourceURL)
# close the downloads tab2
driver.close()
# switch back to main window
driver.switch_to.window(driver.window_handles[0])

如果需要,可以将其作为方法并在需要的地方调用.

if you want you can make it as a method and call where ever it's required.

不必担心,直到下载完成

您可以中继chrome下载状态,请检查以下方法.

You can relay on chrome downloads status, check the below method.

只需在代码中调用以下方法,同时获取文件名

Just call the below method in you code while getting the file name

def getDownLoadedFileName(waitTime):
    downloadsList = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot")
    endTime = time.time()+waitTime
    while True:
        try:
            fileName = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content  #file-link').text")
            if fileName:
                return fileName
        except:
            pass
        time.sleep(1)
        if time.time() > endTime:
            break

您可以按如下所示调用此方法.

You can call this method as shown below.

# wait until the download completes and get the file name
fileName = getDownLoadedFileName(180)
print(fileName)

Firefox :将以下方法用于Firefox.

Firefox: Use the below method for firefox.

def getDownLoadedFileName(waitTime):
    driver.execute_script("window.open()")
    WebDriverWait(driver,10).until(EC.new_window_is_opened)
    driver.switch_to.window(driver.window_handles[-1])
    driver.get("about:downloads")

    endTime = time.time()+waitTime
    while True:
        try:
            fileName = driver.execute_script("return document.querySelector('#contentAreaDownloadsView .downloadMainArea .downloadContainer description:nth-of-type(1)').value")
            if fileName:
                return fileName
        except:
            pass
        time.sleep(1)
        if time.time() > endTime:
            break

Java + Chrome::如果您正在寻找Java实现.

Java + Chrome: In case if you are looking for java implementation.

这是Java中的方法.

Here is the method in java.

public String waitUntilDonwloadCompleted(WebDriver driver) throws InterruptedException {
      // Store the current window handle
      String mainWindow = driver.getWindowHandle();

      // open a new tab
      JavascriptExecutor js = (JavascriptExecutor)driver;
      js.executeScript("window.open()");
     // switch to new tab
    // Switch to new window opened
      for(String winHandle : driver.getWindowHandles()){
          driver.switchTo().window(winHandle);
      }
     // navigate to chrome downloads
      driver.get("chrome://downloads");

      JavascriptExecutor js1 = (JavascriptExecutor)driver;
      // wait until the file is downloaded
      Long percentage = (long) 0;
      while ( percentage!= 100) {
          try {
              percentage = (Long) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#progress').value");
              //System.out.println(percentage);
          }catch (Exception e) {
            // Nothing to do just wait
        }
          Thread.sleep(1000);
      }
     // get the latest downloaded file name
      String fileName = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text");
     // get the latest downloaded file url
      String sourceURL = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').href");
      // file downloaded location
      String donwloadedAt = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div.is-active.focus-row-active #file-icon-wrapper img').src");
      System.out.println("Download deatils");
      System.out.println("File Name :-" + fileName);
      System.out.println("Donwloaded path :- " + donwloadedAt);
      System.out.println("Downloaded from url :- " + sourceURL);
     // print the details
      System.out.println(fileName);
      System.out.println(sourceURL);
     // close the downloads tab2
      driver.close();
     // switch back to main window
      driver.switchTo().window(mainWindow);
      return fileName;
  }

这是在Java脚本中调用它的方法.

This is how to call this in your java script.

// download triggering step 
downloadExe.click();
// now waituntil download finish and then get file name
System.out.println(waitUntilDonwloadCompleted(driver));

这篇关于如何获取正在下载的URL或文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆