我正在使用一个selenium脚本,在这里我试图下载一个Excel文件并给它一个特定的名称。这是我的代码:
我能给正在下载的文件起一个特定的名字吗?
代码:
#!/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
有件事我可以纠正@parishodak的回答:
这里的文件名只返回相对路径(这里是文件名),而不是绝对路径。
这就是@FreshRamen在之后出现以下错误的原因:
有正确的代码:
这里是另一个简单的解决方案,您可以等到下载完成,然后从chrome下载中获取下载的文件名。
铬:
火狐:
单击下载链接/按钮后,只需调用上面的方法。
JAVA+浏览器:
这是java中的方法。
这就是如何在java脚本中调用它。
输出:
不能通过selenium指定下载文件的名称。但是,您可以下载该文件,在下载的文件夹中找到最新的文件,然后根据需要重命名。
注意:从google搜索中借用的方法可能有错误。但你明白了。
相关问题 更多 >
编程相关推荐