[JAVA] Automate IE11 / Edge file downloads with Selenium WebDriver

Problem (button to press)

When downloading a file with IE11 / Edge, a response dialog (notification bar) will appear at the bottom and you need to press "Save".

This dialog. image.png image.png

This time, I investigated how to test this with Selenium WebDriver. The implementation language will be Java.

Code to press the button

It seems that the dialog cannot be hidden in the browser settings, so I investigated how to press "Save".

Refer to the code of Testing Downloading Files in IE9 via Selenium I created it in, but I had to modify it a little because the IE version was different.

sample.java


import java.awt.AWTException;
import java.awt.Robot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

//・ ・ ・

//Element that performs download processing(Button etc.)search for
WebElement downloadElement = driver.findElement(By.id("download"));

try {
    Robot robot = new Robot();

    //Move focus to target element
    downloadElement.sendKeys("""");

    //Press Enter
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

    //Download notification bar(Download manager)Wait for
    Thread.sleep(2000);

    // Alt+Press N to download notification bar(Download manager)Move focus to
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_N);
    robot.keyRelease(KeyEvent.VK_N);
    robot.keyRelease(KeyEvent.VK_ALT);

    //Press Enter to start downloading
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

    //Wait for the download to finish
    Thread.sleep(2000);

    // Alt+Press N to download notification bar(Download manager)Move focus to
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_N);
    robot.keyRelease(KeyEvent.VK_N);        
    robot.keyRelease(KeyEvent.VK_ALT);

    //Press the tab key three times to get to the notification bar"X"Transition to a button
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);

    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);

    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);

    //Press Enter to notify bar(Download manager)Close
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
} catch (AWTException e) {
    e.printStackTrace();
}

Description and notes

As I wrote in the comments, this code uses Robot to perform the keystrokes required for the download. In particular

--Press Alt + N (focus on download dialog) → press Enter (press save button) → press Alt + N again (focus on confirmation dialog after download) → move to X (close) button on tab → Press Enter (dialog closes)

I am doing the operation.

Therefore, if you change the active window during test execution, the key will be operated in that window and the test will fail </ font> (for example, it can be executed normally). If you open the download folder to check if it is, the key will be operated there). Be careful not to switch the active window while testing the code here.

Also, if the specifications of the browser change in the future and the key operation changes, it needs to be corrected. Finally, I will list the browser version confirmed this time.

IE11 version information: image.png

Edge version: image.png

Recommended Posts

Automate IE11 / Edge file downloads with Selenium WebDriver
Summary of problems and countermeasures when operating IE with WebDriver of Selenium2
Read xlsx file in Java with Selenium
Run Edge (Chromium version) on Mac with Selenium