Python Selenium File Download

Source: Internet
Author: User

Python Selenium into the UI Automation test will encounter file upload and download operations, the following describes the file download operation

This article describes the operation of downloading files using the Firefox browser.

1. Set file default

For example, Firefox can view or modify configuration information by entering: About:config or about:aupport in the address bar.


2. Set up automatic download operation

There are two ways of solving this.

2.1. Set automatic save download

If checked: Automatically use the same action in the future to process such files

This will not be the next time you download a file of that type.

2.2, modify the settings can be modified in the browser options

Such as

3. Download the file

This setting is complete, but the browser opened when the program starts does not open in this configuration.

By contrast, the browser opened by Webdriver differs from the manually opened browser, because the browser opened by Webdriver is not opened according to the configuration file set by the browser. If you want to follow the profile, get the profile information before you open it.

There are two ways to resolve this:

3.1. Add the following code:

      Profile =  Webdriver. Firefoxprofile (R"C:\Users\Skyyj\AppData\Roaming\Mozilla\Firefox\Profiles\1rzh6139.default " )        = Webdriver. Firefox (Profile)

3.2. Adding configuration information to the code

by About:config

Find the default configuration via%appdata%\mozilla\firefox\profiles\

Find the MIMETYPES.RDF directory and open it in another way to find the type of file you just saved

MIMETYPES.RDF is present in the configuration path of the above profile

C:\Users\Skyyj\AppData\Roaming\Mozilla\Firefox\Profiles\1rzh6139.default

Find fileextensions= "xlsx"

Nc:value= "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Nc:editable= "true"
Nc:fileextensions= "xlsx"
nc:description= "xlsx File" >

You can tell from the file what type of file we need


Insert the following code:

#Profile = Webdriver. Firefoxprofile (r "C:\Users\Skyyj\AppData\Roaming\Mozilla\Firefox\Profiles\1rzh6139.default")Profile =Webdriver. Firefoxprofile ()##设置成0代表下载到浏览器默认下载路径, set to 2 to save to the specified directoryProfile.set_preference ("browser.download.folderList", 2)        #the settings here or not are not affected and no effect is found.         #profile.set_preference ("browser.download.manager.showWhenStarting", False)Profile.set_preference ("Browser.download.dir"R"C:\Down") Profile.set_preference ("Browser.helperApps.neverAsk.saveToDisk","Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")        #The setting here or not has no effect on finding        #profile.set_preference ("Browser.helperApps.alwaysAsk.force", False);Self.driver = Webdriver. Firefox (Profile)

It is recommended to use the second one, so that you do not need to set up a browser, portability is good.


Python Selenium File Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.