Selenium give file name when downloading

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 ?

Code:

#!/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

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([f for f in os.listdir('c:downloads')], key=os.path.getctime)
shutil.move(os.path.join(dirpath,filename),newfilename)

There is something i would correct for @parishodak answer:

the filename here will only return the relative path (here the name of the file) not the absolute path.

That is why @FreshRamen got the following error after:

File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/‌​python2.7/genericpath.py", 
line 72, in getctime return os.stat(filename).st_ctime OSError: 
[Errno 2] No such file or directory: '.localized'

There is the correct code:

import os
import shutil

filepath = 'c:downloads'
filename = max([filepath +""+ f for f in os.listdir(filepath)], key=os.path.getctime)
shutil.move(os.path.join(dirpath,filename),newfilename)

Hope this snippet is not that confusing. It took me a while to create this and is really useful, because there has not been a clear answer to this problem, with just this library.

import os
import time
def tiny_file_rename(newname, folder_of download):
    filename = max([f for f in os.listdir(folder_of download)], key=lambda xa :   os.path.getctime(os.path.join(folder_of_download,xa)))
    if '.part' in filename:
        time.sleep(1)
        os.rename(os.path.join(folder_of download, filename), os.path.join(folder_of download, newname))
    else:
        os.rename(os.path.join(folder_of download, filename),os.path.join(folder_of download,newname))

Hope this saves someone's day, cheers.

EDIT: Thanks to @Om Prakash editing my code, it made me remember that I didn't explain the code thoughly.

Using the max([]) function could lead to a race condition, leaving you with empty or corrupted file(I know it from experience). You want to check if the file is completely downloaded in the first place. This is due to the fact that selenium don't wait for the file download to complete, so when you check for the last created file, an incomplete file will show up on your generated list and it will try to move that file. And even then, you are better off waiting a little bit for the file to be free from Firefox.

链接地址: http://www.djcxy.com/p/89532.html

上一篇: 如何在android中为单元测试模拟MotionEvent和SensorEvent?

下一篇: 硒在下载时提供文件名