Download a file in IE using Selenium

后端 未结 6 1446
忘掉有多难
忘掉有多难 2020-12-06 08:35

OK, so I am trying to export a file using Selenium. My browser is IE. When I click on the export button a native windows dialogue box comes up.

Image of the pop up

6条回答
  •  渐次进展
    2020-12-06 09:03

    This is a hack by Dave Haefner. If you don't care if a file was downloaded or not and you want to confirm only that a file can be downloaded, you can use an HTTP request. Instead of downloading the file you'll receive the header information for the file which contains things like the content type and length. With this information, you can confirm the file is you expect.

        String link = driver.findElement(By.cssSelector("download-link-element")).getAttribute("href");
    
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpHead request = new HttpHead(link);
        HttpResponse response = httpClient.execute(request);
        String contentType = response.getFirstHeader("Content-Type").getValue();
        int contentLength = Integer.parseInt(response.getFirstHeader("Content-Length").getValue());
    
        assertThat(contentType, is("application/octet-stream"));
        assertThat(contentLength, is(not(0)));
    

提交回复
热议问题