硒browser.find_element_by_id.单击下载的文件名



如果我有一个python脚本,单击页面上的按钮自动下载包含一堆数据的 excel 工作表,如何获取下载文件名和位置?

网络驱动程序的代码:

from selenium import webdriver
browser = webdriver.Chrome(executable_path=r"C:/Program Files (x86)/Google/Chrome/Application/chromedriver")

在调出页面后,我执行以下操作:

download_elem = browser.find_element_by_id("imgPrintToExcel")
download_elem.click()

这运行良好,excel文件使用默认文件名下载到我的本地"下载"文件夹中。无论如何都可以更改下载文件的默认名称,还是必须通过转到目录并更改文件名来执行此操作?

如果要更改下载目录:

from selenium import webdriver
options = webdriver.ChromeOptions() 
directory = "C:/Downloads"
options.add_argument("download.default_directory=directory)
driver = webdriver.Chrome(chrome_options=options)

如果要更改文件名:

import re
import os
from pathlib import Path
directory = "C:/Downloads" # download directory
download_elem = browser.find_element_by_id("imgPrintToExcel")
path = link.get_attribute("href") # this will give us the link of file make sure it is the correct attribute for you
filename = re.findall(r'.*/(.*?)$', path)[0] # we want just the filename so we will parse it
# in order to avoid mistaking our file we will check if there is a file with same name beforehand
if Path("%s/%s" % (directory, filename)).is_file(): # checking if filename exist
name = ""
type = ""
if ('.' in filename): # checking if filename with type like .exe
(name, type) = filename.split('.')
type = "."+type
else:
name = filename
filename = "%s(1)%s" % (name, type)
i = 1
while(True): # checking if there is other file duplicates
if Path("%s/%s(%d)%s" % (directory, name, i, type)).is_file():
i += 1
filename = "%s(%d)%s" % (name, i, type)
else:
break;
else:
pass
download_elem.click()
# wait for download to finish
os.rename(r"%s/%s" % (directory, filename),r"%s/new_filename" % directory')

为浏览器设置下载文件夹并等待新文件...
Web驱动程序的代码(java(:

HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("download.default_directory", DOWNLOADDIR);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
ChromeDriver driver = new ChromeDriver(options);

最新更新