Selenium 下载时给出文件名 [英] Selenium give file name when downloading

查看:244
本文介绍了Selenium 下载时给出文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 selenium 脚本,尝试下载 Excel 文件并为其指定特定名称.这是我的代码:

I am working with a selenium script where I am trying to download a Excel file and give it a specific name. This is my code:

无论如何我可以给正在下载的文件一个特定的名称吗?

Is there anyway that I can give the file being downloaded a specific name ?

代码:

#!/usr/bin/python
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

profile = FirefoxProfile()
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream")
profile.set_preference("browser.download.dir", "C:\Downloads" )
browser = webdriver.Firefox(firefox_profile=profile)

browser.get('https://test.com/')
browser.find_element_by_partial_link_text("Excel").click() # Download file

推荐答案

您不能通过 selenium 指定下载文件的名称.但是,您可以下载文件,在下载的文件夹中找到最新的文件,然后根据需要重命名.

You cannot specify name of download file through selenium. However, you can download the file, find the latest file in the downloaded folder, and rename as you want.

注意:从谷歌搜索中借用的方法可能有错误.但你懂的.

Note: borrowed methods from google searches may have errors. but you get the idea.

import os
import shutil
filename = max([Initial_path + "\" + f for f in os.listdir(Initial_path)],key=os.path.getctime)
shutil.move(filename,os.path.join(Initial_path,r"newfilename.ext"))

这篇关于Selenium 下载时给出文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆