Tips of "teach you by hand" - java+ selenium automated testing - download files - Part 2 (detailed tutorial)

Posted by choubix on Thu, 13 Jan 2022 02:22:49 +0100

1. Introduction

The previous articles explained how to upload files. Since there is upload, there may be download files. Therefore, brother Hong will continue to explain and share: automatic test download files. Some friends or children's shoes may think that this is not very simple. You can also introduce and explain it with you. Not to mention, you can visit the download page, locate the download button of the file to be downloaded, and then click the button. In fact, it's not like this. Let's listen to brother Hong Xu: brother Hong's download here is to remove the download pop-up box.

2. Remove the advantages of the download pop-up window

(1) Retrieve the import of keyboard and mouse automation control module
(2) It can run headless without affecting other tasks at the same time

3.Chrome automatic download files

3.1 parameter description

Compared with Firefox, the download window of Chrome will not pop up by default. We mainly want to modify the default download path of Chrome.
Chrome's settings look more complex than Firefox's, but you need to focus on two settings.

Similar to Chrome browser, set its options:

download.default_directory: Set download path
profile.default_content_settings.popups: Set to 0 Disable pop ups

3.2 code design

3.3 reference code

package lessons;

import org.openqa.selenium.By; 
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.remote.CapabilityType; 
import org.openqa.selenium.remote.DesiredCapabilities; 

import java.util.HashMap; 

 * @author Beijing - Hongge
 * @Official account: Beijing, Hong brother
 * @<Teach you by hand series tips (56) - java+ selenium automated testing - download files - Part 1 (detailed tutorial)
 * @2021 December 19
public class ChromeDownload {
    public static void main(String[] args) throws InterruptedException { 
        String downloadFilepath = "D:\\test2"; 
        HashMap<String, Object> chromePrefs = new HashMap<String, Object>(); 
        chromePrefs.put("profile.default_content_settings.popups", 0); 
        chromePrefs.put("download.default_directory", downloadFilepath); 
        ChromeOptions options = new ChromeOptions(); 
        HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>(); 
        DesiredCapabilities cap =; 
        cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap); 
        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); 
        cap.setCapability(ChromeOptions.CAPABILITY, options); 
        WebDriver driver = new ChromeDriver(cap); 
        driver.get("");//To target page      
        WebElement myElement = driver.findElement(By.xpath("//a[contains(text(),'selenium-4.1.0-py3-none-any.whl')]"));
        Actions action = new Actions(driver);;//Click download
        System.out.println("browser will be close");


3.4 operation code

1. Run the code, right-click run as - > java appliance, and the console will output, as shown in the following figure:

2. After running the code, the action of the browser on the computer side is shown in the following small video:

4. Summary

Originally, the next article was intended to introduce and explain IE browser, but I checked a lot of materials and tried various methods (including keyboard simulation and autoIT on the Internet) which failed, so I didn't write the download file about IE browser. If you have a clear message, you can leave a message to Hongge ha!!! But there are two browser methods, enough.