栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何获取正在下载的URL或文件名?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何获取正在下载的URL或文件名?

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

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

如果您希望脚本 等待下载 完成,请

getDownLoadedFileName
在答案末尾检查 方法。

# open a new tabdriver.execute_script("window.open()")# switch to new tabdriver.switch_to.window(driver.window_handles[-1])# navigate to chrome downloadsdriver.get('chrome://downloads')# get the latest downloaded file namefileName = 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 urlsourceURL = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content  #file-link').href")# print the detailsprint(fileName)print (sourceURL)# close the downloads tab2driver.close()# switch back to main windowdriver.switch_to.window(driver.window_handles[0])

如果您愿意,可以将其作为方法并在需要的地方调用。

编辑:不要担心,如果您必须等到下载完成

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

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

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

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

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

Firefox: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中的方法。

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脚本中调用此方法的方法。

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646733.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号