无法使用HtmlUnitDriver[Selenium WebDriver java]拍摄屏幕截图

无法使用HtmlUnitDriver[Selenium WebDriver java]拍摄屏幕截图,java,selenium,selenium-webdriver,htmlunit-driver,Java,Selenium,Selenium Webdriver,Htmlunit Driver,我想用HtmlUnitDriver截图一个页面,我遇到了这个家伙,他制作了一个自定义的HTML单元驱动程序来截图。 但不幸的是,在实现这一点时,我遇到了一个例外 线程“main”java.lang.ClassCastException中的异常:[B不能转换为java.io.File 在Test.main(Test.java:39)”中 我的代码如下- import java.io.File; import java.io.IOException; import org.openqa.seleni

我想用HtmlUnitDriver截图一个页面,我遇到了这个家伙,他制作了一个自定义的HTML单元驱动程序来截图。 但不幸的是,在实现这一点时,我遇到了一个例外

线程“main”java.lang.ClassCastException中的异常:[B不能转换为java.io.File 在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驱动程序(链接中的一个)是-


该错误表示代码正试图将
字节[]
转换为
文件
。如果只从
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 scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C:/Users/home/Desktop/screenshot.png"));// copy it somewhere

你能添加完整的异常堆栈并说出“B”的类型吗?嗨,Florent,我编辑了代码并添加了带有printstacktrace的try catch,但我仍然得到“java.lang.ClassCastException:[B不能在Test.main(Test.java:19)”处转换为java.io.File作为stacktraceHi,如果不是的话,有必要使用HtmlUnitDriver吗?请选择Phantom js。在谈论屏幕快照时,它会更好。我不熟悉Phantom js!我们可以将Phantom js与selenium web driver一起使用吗?因为上面的代码只是我试图通过无头浏览器拍摄网页截图的较大代码的一部分n我想使用Html单元驱动程序是因为它的速度。虽然phantom.js也比chrome和firefox快,但它不如HtmlUnit驱动程序快!这并没有解释/处理实际的错误消息,误解了问题和OP试图实现的目标,并将一个错误替换为另一个错误。这是解决问题的最简单方法ke当前网页的屏幕截图这不是问题所在。OP想知道如何通过“ClassCastException:[B不能转换为java.io.File”在一个定制版本的HtmlUnitDriver中,它通常根本不能截图。非常感谢你,安德鲁,这很好:)我在我的问题中添加了一些细节,你能看一下吗?非常感谢。
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;
}
byte[] zipFileBytes = ((ScreenCaptureHtmlUnitDriver) driver).getScreenshotAs(OutputType.BYTES);
FileUtils.writeByteArrayToFile(new File("D:\\TEMP.PNG"), zipFileBytes);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C:/Users/home/Desktop/screenshot.png"));// copy it somewhere