【问题标题】:How to download a file from IE browser using webdriver-selenium如何使用 webdriver-selenium 从 IE 浏览器下载文件
【发布时间】:2023-03-21 09:01:01
【问题描述】:

我正在尝试使用 webdriver 为我的 Web 应用程序实现下载文件功能。在 chrome 或 firefox 中,我们可以设置在单击下载按钮时将文件下载到定义位置的功能,而无需任何窗口提示。

但在 IE 9 中,我没有找到下载选项的任何功能设置。

在 IE 中单击下载按钮后,webdriver 在窗口提示保存/打开/取消后挂起。点击操作后 Webdriver 不返回任何内容。我不得不退出 webdriver 并重新创建 webdriver 对象以继续下一步。

为了处理窗口提示,我尝试使用 Sikuli/send key 方法等。所有这些仅在窗口处于焦点时有效。当我在机器锁定的情况下运行脚本或通过 Jenkins 等远程运行时,它不起作用。

如何配置 IE no 以提示保存选项但将文件下载到任何预定义的路径。和IE8一样,我们可以设置注册表来设置下载路径。

【问题讨论】:

  • 通常你得到文件的url,然后你调用一些不是webdriver函数来检索给定url的文件(在python中是urllib.urlretrieve(url,path)或类似的东西),你用的是什么编程语言?

标签: internet-explorer selenium download webdriver


【解决方案1】:

如果存在文件下载弹出窗口,Selenium 会抛出 Modal dialog Present 异常。有时 webdriver 会根据弹出窗口出现后在浏览器上执行的操作而挂起。

如果需要,您可以通过注册表手动完成: http://9to5it.com/internet-explorer-disable-do-you-want-to-open-or-save-this-file-prompt/

如果您使用 Java 作为编码语言,则可以使用 Robot 类。例如,请参阅下面的链接。 How to download .docx file using Selenium webdriver in Java?

c# 中没有类,就像 Java 中的 Robot 类一样。我正在使用AutoIT 来处理窗口弹出窗口。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    要在 IE - 浏览器中下载任何文件,最好使用 cookie 来完成。请使用 cookie,而不是浪费时间寻找其他方式。

    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 {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 2012-06-03
      • 2012-05-30
      相关资源
      最近更新 更多