Java - 下载时IE 9浏览器中的NullPointerException

时间:2013-12-18 11:26:35

标签: java selenium selenium-webdriver

我在使用Java中的Webdriver在IE9浏览器中下载Excel文件时收到NullPointerException。下面是我下载Excel的代码。

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Set;

public class FileDownloader {

private static final Logger LOG = Logger.getLogger(FileDownloader.class);
private WebDriver driver;
private String localDownloadPath = System.getProperty("java.io.tmpdir");
private boolean followRedirects = true;
private int httpStatusOfLastDownloadAttempt;

public FileDownloader(WebDriver driverObject) {
    this.driver = driverObject;
}

public void followRedirectsWhenDownloading(boolean value) {
    this.followRedirects = value;
}

public String localDownloadPath() {
    return this.localDownloadPath;
}

public void localDownloadPath(String filePath) {
    this.localDownloadPath = filePath;
}

public String downloadFile(WebElement element) throws Exception {
    return downloader(element, "href");
}

public String downloadImage(WebElement element) throws Exception {
    return downloader(element, "src");
}

public int httpStatusOfLastDownloadAttempt() {
    return this.httpStatusOfLastDownloadAttempt;
}

private HttpState mimicCookieState(Set<org.openqa.selenium.Cookie> seleniumCookieSet) {
    HttpState mimicWebDriverCookieState = new HttpState();
    for (org.openqa.selenium.Cookie seleniumCookie : seleniumCookieSet) {
        Cookie httpClientCookie = new Cookie(seleniumCookie.getDomain(), seleniumCookie.getName(), seleniumCookie.getValue(), seleniumCookie.getPath(), seleniumCookie.getExpiry(), seleniumCookie.isSecure());
        mimicWebDriverCookieState.addCookie(httpClientCookie);
    }

    return mimicWebDriverCookieState;
}

private HostConfiguration setHostDetails(String hostURL, int hostPort) {
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(hostURL, hostPort);

    return hostConfig;
}

private String downloader(WebElement element, String attribute) throws IOException, NullPointerException {
    String fileToDownloadLocation = element.getAttribute(attribute);
    if (fileToDownloadLocation.trim().equalsIgnoreCase("")) throw new NullPointerException("The element you have specified does not link to anything!");

    URL fileToDownload = new URL(fileToDownloadLocation);
    File downloadedFile = new File(this.localDownloadPath + fileToDownload.getFile().replaceFirst("/|\\\\", ""));
    if (downloadedFile.canWrite() == false) downloadedFile.setWritable(true);

    HttpClient client = new HttpClient();
    client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
    client.setHostConfiguration(setHostDetails(fileToDownload.getHost(), fileToDownload.getPort()));
    client.setState(mimicCookieState(this.driver.manage().getCookies()));
    HttpMethod getFileRequest = new GetMethod(fileToDownload.getPath());
    getFileRequest.setFollowRedirects(this.followRedirects);
    LOG.info("Follow redirects when downloading: " + this.followRedirects);

    LOG.info("Sending GET request for: " + fileToDownload.toExternalForm());
    this.httpStatusOfLastDownloadAttempt = client.executeMethod(getFileRequest);
    LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt);
    LOG.info("Downloading file: " + downloadedFile.getName());
    FileUtils.copyInputStreamToFile(getFileRequest.getResponseBodyAsStream(), 
    downloadedFile);
    getFileRequest.releaseConnection();

    String downloadedFileAbsolutePath = downloadedFile.getAbsolutePath();
    LOG.info("File downloaded to '" + downloadedFileAbsolutePath + "'");

    return downloadedFileAbsolutePath;
}
}

我可以使用下面的代码点击下载图片按钮,但在将内容下载到文件中时会收到NullPointerException。

FileDownloader downloadTestFile = new FileDownloader(driver);
driver.get("https://www.abc.com/dasdetail.aspx");
WebElement downloadLink = driver.findElement(By.id(OR.getProperty("imgExcel_ID")));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", downloadLink);

String downloadedFileAbsoluteLocation = downloadTestFile.downloadFile(downloadLink);

我发送的网址为&#34; https://www.abc.com/dasdetail.aspx&#34;和属性ID为&#34; ctl00_MasterPlaceHolder_ImgExcel &#34;要下载excel按钮,仍然会在位置获取NullPointerException:enter image description here

我用谷歌搜索满足我的要求并找到上面显示的方法downloadFile(),请让我知道我做错了什么。我无法追查我遇到的问题,请在这种情况下帮助我。

0 个答案:

没有答案