如果我有一个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文件以默认文件名下载到我的本地“下载”文件夹中。无论如何,是否可以更改下载文件的默认名称,还是必须通过转到目录并更改文件名来做到这一点?
答案 0 :(得分:1)
如果要更改下载目录:
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')
答案 1 :(得分:0)
设置浏览器的下载文件夹并等待新文件...
用于webdriver的代码(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);