无法使用HtmlUnitDriver进行屏幕截图 [Selenium WebDriver java]

4
我想使用HtmlUnitDriver截取页面的屏幕截图,我在这个链接上看到一个人创建了自定义的HTML单元驱动程序来截图。但不幸的是,在实现时我遇到了异常。

"Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to java.io.File at Test.main(Test.java:39)"

我的代码如下-

import java.io.File;
import java.io.IOException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import com.gargoylesoftware.htmlunit.BrowserVersion;

public class Test extends ScreenCaptureHtmlUnitDriver {

    public static void main(String[] args) throws InterruptedException, IOException {

        WebDriver driver = new ScreenCaptureHtmlUnitDriver(BrowserVersion.FIREFOX_38);
        driver.get("https://www.google.com/?gws_rd=ssl");
        try{
        File scrFile = ((ScreenCaptureHtmlUnitDriver) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("D:\\TEMP.PNG"));
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我正在使用的HtmlUnit驱动程序(链接中的那个)是这个-
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.internal.Base64Encoder;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class ScreenCaptureHtmlUnitDriver extends HtmlUnitDriver implements TakesScreenshot {

private static Map<String, byte[]> imagesCache = Collections.synchronizedMap(new HashMap<String, byte[]>());

private static Map<String, String> cssjsCache = Collections.synchronizedMap(new HashMap<String, String>());

// http://stackoverflow.com/questions/4652777/java-regex-to-get-the-urls-from-css
private final static Pattern cssUrlPattern = Pattern.compile("background(-image)?[\\s]*:[^url]*url[\\s]*\\([\\s]*([^\\)]*)[\\s]*\\)[\\s]*");// ?<url>

public ScreenCaptureHtmlUnitDriver() {
    super();
}

public ScreenCaptureHtmlUnitDriver(boolean enableJavascript) {
    super(enableJavascript);
}

public ScreenCaptureHtmlUnitDriver(Capabilities capabilities) {
    super(capabilities);
}

public ScreenCaptureHtmlUnitDriver(BrowserVersion version) {
    super(version);
    DesiredCapabilities var = ((DesiredCapabilities) getCapabilities());
    var.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
}

//@Override
@SuppressWarnings("unchecked")
public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException {
    byte[] archive = new byte[0];
    try {
        archive = downloadCssAndImages(getWebClient(), (HtmlPage) getCurrentWindow().getEnclosedPage());
    } catch (Exception e) {
    }
    if(target.equals(OutputType.BASE64)){
        return target.convertFromBase64Png(new Base64Encoder().encode(archive));
    }
    if(target.equals(OutputType.BYTES)){
        return (X) archive;
    }
    return (X) archive;
}

// https://dev59.com/c3E95IYBdhLWcg3wlu-z
protected byte[] downloadCssAndImages(WebClient webClient, HtmlPage page) throws Exception {
    WebWindow currentWindow = webClient.getCurrentWindow();
    Map<String, String> urlMapping = new HashMap<String, String>();
    Map<String, byte[]> files = new HashMap<String, byte[]>();
    WebWindow window = null;
    try {
        window = webClient.getWebWindowByName(page.getUrl().toString()+"_screenshot");
        webClient.getPage(window, new WebRequest(page.getUrl()));
    } catch (Exception e) {
        window = webClient.openWindow(page.getUrl(), page.getUrl().toString()+"_screenshot");
    }

    String xPathExpression = "//*[name() = 'img' or name() = 'link' and (@type = 'text/css' or @type = 'image/x-icon') or  @type = 'text/javascript']";
    List<?> resultList = page.getByXPath(xPathExpression);

    Iterator<?> i = resultList.iterator();
    while (i.hasNext()) {
        try {
            HtmlElement el = (HtmlElement) i.next();
            String resourceSourcePath = el.getAttribute("src").equals("") ? el.getAttribute("href") : el
                    .getAttribute("src");
            if (resourceSourcePath == null || resourceSourcePath.equals(""))
                continue;
            URL resourceRemoteLink = page.getFullyQualifiedUrl(resourceSourcePath);
            String resourceLocalPath = mapLocalUrl(page, resourceRemoteLink, resourceSourcePath, urlMapping);
            urlMapping.put(resourceSourcePath, resourceLocalPath);
            if (!resourceRemoteLink.toString().endsWith(".css")) {
                byte[] image = downloadImage(webClient, window,  resourceRemoteLink);
                files.put(resourceLocalPath, image);
            } else {
                String css = downloadCss(webClient, window, resourceRemoteLink);
                for (String cssImagePath : getLinksFromCss(css)) {
                    URL cssImagelink = page.getFullyQualifiedUrl(cssImagePath.replace("\"", "").replace("\'", "")
                            .replace(" ", ""));
                    String cssImageLocalPath = mapLocalUrl(page, cssImagelink, cssImagePath, urlMapping);
                    files.put(cssImageLocalPath, downloadImage(webClient, window, cssImagelink));
                }
                files.put(resourceLocalPath, replaceRemoteUrlsWithLocal(css, urlMapping)
                        .replace("resources/", "./").getBytes());
            }
        } catch (Exception e) {
        }
    }
    String pagesrc =  replaceRemoteUrlsWithLocal(page.getWebResponse().getContentAsString(), urlMapping);
    files.put("page.html", pagesrc.getBytes());
    webClient.setCurrentWindow(currentWindow);
    return createZip(files);
}

String downloadCss(WebClient webClient, WebWindow window, URL resourceUrl) throws Exception {
    if (cssjsCache.get(resourceUrl.toString()) == null) {
        cssjsCache.put(resourceUrl.toString(), webClient.getPage(window, new  WebRequest(resourceUrl))
                .getWebResponse().getContentAsString());

    }
    return cssjsCache.get(resourceUrl.toString());
}

byte[] downloadImage(WebClient webClient, WebWindow window, URL resourceUrl)  throws Exception {
    if (imagesCache.get(resourceUrl.toString()) == null) {
        imagesCache.put(
                resourceUrl.toString(),
                IOUtils.toByteArray(webClient.getPage(window, new  WebRequest(resourceUrl)).getWebResponse()
                        .getContentAsStream()));
    }
    return imagesCache.get(resourceUrl.toString());
}

 public static byte[] createZip(Map<String, byte[]> files) throws IOException      {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zipfile = new ZipOutputStream(bos);
    Iterator<String> i = files.keySet().iterator();
    String fileName = null;
    ZipEntry zipentry = null;
    while (i.hasNext()) {
        fileName = i.next();
        zipentry = new ZipEntry(fileName);
        zipfile.putNextEntry(zipentry);
        zipfile.write(files.get(fileName));
    }
    zipfile.close();
    return bos.toByteArray();
}

    List<String> getLinksFromCss(String css) {
    List<String> result = new LinkedList<String>();
    Matcher m = cssUrlPattern.matcher(css);
    while (m.find()) { // find next match
        result.add( m.group(2));
    }
    return result;
}

 String replaceRemoteUrlsWithLocal(String source, Map<String, String>  replacement) {
    for (String object : replacement.keySet()) {
        // background:url(http://org.com/images/image.gif)
        source = source.replace(object, replacement.get(object));
    }
    return source;
}

String mapLocalUrl(HtmlPage page, URL link, String path, Map<String, String>  replacementToAdd) throws Exception {
    String resultingFileName = "resources/" +    FilenameUtils.getName(link.getFile());
    replacementToAdd.put(path, resultingFileName);
    return resultingFileName;
}

}

更新

安德鲁提供的代码是可行的,但我想知道是否有一种方法可以仅下载所选资源。例如,在网站上,我只想下载id为“//*[@id='cimage']”的验证码图片,因为下载所有资源需要很长时间。是否有一种方法可以仅下载特定的资源?因为使用下面提供的现有代码会下载所有资源。

byte[] zipFileBytes = ((ScreenCaptureHtmlUnitDriver) driver).getScreenshotAs(OutputType.BYTES);
FileUtils.writeByteArrayToFile(new File("D:\\TEMP.PNG"), zipFileBytes);

你能添加完整的异常堆栈并告诉“B”的类型吗? - Florent B.
嗨,Florent。我编辑了代码并添加了try catch和printstacktrace,但我仍然得到“java.lang.ClassCastException:[B无法转换为java.io.File 在Test.main(Test.java:19)”作为堆栈跟踪。 - Ajay
嗨,如果不需要HtmlUnitDriver,请使用Phantom js,它在截屏方面更好。 - eduliant
我不熟悉phantom js!我们能否在selenium web driver中使用phantom js?因为上面的代码只是更大代码的一部分,我正在尝试通过无头浏览器截取网页的屏幕截图。 - Ajay
我想使用HtmlUnit驱动程序的原因是它速度快。虽然Phantom.js比Chrome和Firefox也要快,但它不如HtmlUnit驱动程序快! - Ajay
2个回答

2
错误提示是代码试图将byte[]转换为File。如果您从getScreenshotAs中剔除未使用的路径,那么这种错误就很容易理解了。
public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException {
    byte[] archive = new byte[0];
    try {
        archive = downloadCssAndImages(getWebClient(), (HtmlPage) getCurrentWindow().getEnclosedPage());
    } catch (Exception e) {
    }
    return (X) archive;
}

你无法从中获取File。不支持OutputType.FILE,因此您必须自己处理文件输出。幸运的是,这很容易。您可以将代码更改为:

byte[] zipFileBytes = ((ScreenCaptureHtmlUnitDriver) driver).getScreenshotAs(OutputType.BYTES);
FileUtils.writeByteArrayToFile(new File("D:\\TEMP.PNG"), zipFileBytes);

请参阅FileUtils.writeByteArrayToFile了解更多信息。

1
非常感谢,安德鲁,这个很好用 :) 我已经在我的问题中添加了更多细节,你能看一下吗?非常感谢。 - Ajay

-3

看看这个,可能对你有帮助

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C:/Users/home/Desktop/screenshot.png"));// copy it somewhere

2
这并没有解释/处理实际的错误信息,误解了问题以及OP试图实现的内容,并且会用另一个错误来替换它。 - Andrew Regan
这是最简单的方法,可以截取当前网页的屏幕截图。 - monil
那不是问题所在。OP 想知道如何在定制版本的 HtmlUnitDriver 中解决“ClassCastException: [B 无法转换为 java.io.File”问题,而这个版本通常根本无法截屏。 - Andrew Regan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接