python获取url文件名_如何获取正在下载的url或文件名?

下面是获取最新下载文件名和url的简单解决方案。在

注意:考虑到文件下载是在运行下面的代码之前完成的。

如果您希望脚本等到下载完成,然后检查答案末尾的getDownLoadedFileName方法。在# 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])

如果需要的话,可以随时调用它。在

编辑:如果要等到下载完成,请不要担心。

你可以中继chrome下载状态,检查下面的方法。在

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

^{pr2}$

您可以如下所示调用此方法。在# wait until the download completes and get the file name

fileName = 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 name

System.out.println(waitUntilDonwloadCompleted(driver));

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值