How to download a file from IE browser using webdr

2020-06-30 02:21发布

I am trying to implement a download file functionality for my web application using webdriver. In chrome or firefox, we can set the capability to download the files to a defined location upon clicking download button without any window prompt.

But in IE 9, I don't find any capability setting for download options.

Also after clicking on download button in IE, webdriver is hanging after window prompt for save/open/cancel. Webdriver doesn't return anything after click operation. I had to quit webdriver and re-create the webdriver object to proceed with next step.

To handle the window prompt, I tried using Sikuli/send key methods etc. All these works only when the window is in focus. It doesn't work when I run the script having machine locked or run through remote like via Jenkins.

How can I configure IE no to prompt save option but download file to any predefined path. Like in IE8, we can set the regedit to set download path.

2条回答
成全新的幸福
2楼-- · 2020-06-30 03:02

To download any file in IE - Browser, it better idea to use the cookies to get it done. Please use cookies rather than spending worthless time finding other way.

List item
URL myUrl = new URL("linkName");
        URLConnection urlConn = myUrl.openConnection();
        urlConn.connect();
        CookieStore cookieStore = seleniumCookiesToCookieStore();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.setCookieStore(cookieStore);
        HttpGet httpGet = new HttpGet(downloadUrl);
        System.out.println("Downloding file form: " + downloadUrl);
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = entity.getContent();
            FileOutputStream fileOutputStream = new FileOutputStream("LocationToSave");
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = inputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, read);
            }
            fileOutputStream.close();
            System.out.println("Downloded " + entity.getContentType());
        } else {}
查看更多
我想做一个坏孩纸
3楼-- · 2020-06-30 03:06

Selenium throws Modal dialog Present exception if file download popup present. Some times webdriver hangs out based on operations perform on the browser after popup present.

You can do it manually through registry if you want: http://9to5it.com/internet-explorer-disable-do-you-want-to-open-or-save-this-file-prompt/

If you are using Java as coding language, you can use Robot class. Please refer below link for example. How to download .docx file using Selenium webdriver in Java?

There is no class in c# as Robot class in Java. I am using AutoIT to handle windows popups.

Hope this helps.

查看更多
登录 后发表回答