简体   繁体   中英

Handling new window in Internet Explorer using selenium webdriver

I am using selenium webdriver to automate a scenario where in clicking on a download image opens a new window. This new window will have the "Save As" dialog box.

As soon as I click on the image, new window opens up but is closed immediately without displaying the "Save as" Dialog box.

My scenario is to make sure the file is present and read the file name by clicking on the save as button.

My code:

WebElement e1= driver.findElement(By.id("id of image"));
e1.click(); 
Set<String> set = driver.getWindowHandles();
List<String> handles = new ArrayList<String>(set);
driver.switchTo().window(handles.get(1)); // switch to file download dialog box

Based on my exp, Seleniun file download test is a pain to automation engineer. Instead of doing step-by-step download, we could use HttpURLConnection, Apache HttpComponents (or maybe just a file get through URL) for the link specified and assert a 200 OK response.

For more information, you can refer to following link for tools to handle this case

https://github.com/Ardesco/Ebselen/blob/master/ebselen-core/src/main/java/com/lazerycode/ebselen/customhandlers/FileDownloader.java

http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM