![](/img/trans.png)
[英]Getting .part evertime i download file in using selenium webdriver in python
[英]Getting the name of file download using selenium in python
因此,我正在使用Selenium下載文件,它可以正常工作,但是我需要獲取文件名。
我的變量path
應獲得下載內容的名稱,但打印出的所有內容均為“ none”。
driver = webdriver.Firefox(firefox_profile=profile, firefox_binary=Binary)
driver.get("stuff")
time.sleep(2)
path = driver.find_element_by_xpath("//a[contains(text(), 'tgz')]").click()
print path
不必所有鏈接都包含文件名(它們有時也可以重定向),因此在這種情況下,您可以嘗試使用os.listdir()
在下載文件之前和之后檢查目錄列表中的差異。 。
import os
before = os.listdir('/home/jason/Downloads')
# Download the file using Selenium here
after = os.listdir('/home/jason/Downloads')
change = set(after) - set(before)
if len(change) == 1:
file_name = change.pop()
else:
print "More than one file or no file downloaded"
首先,您將獲得鏈接對象,因此您可以對其進行處理:
link = driver.find_element_by_xpath("//a[contains(text(), 'DEVcrt.sp1')]")
那么您將從鏈接對象獲取“ href”屬性:
path = link.get_attribute("href") #this will return something like 'www.booger.com/file.exe'
然后單擊鏈接對象:
link.click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.