所以我正在使用 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
Run Code Online (Sandbox Code Playgroud)
这是@TehTris解决方案的替代解决方案。
不一定所有链接都包含文件名(有时也可以重定向),因此在这种情况下,您可以尝试使用 . 下载文件之前和之后检查目录列表中的差异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"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9375 次 |
| 最近记录: |