如何在firefox headless(java中的selenium)中截图?

如何在firefox headless(java中的selenium)中截图?,java,selenium,firefox,selenium-webdriver,Java,Selenium,Firefox,Selenium Webdriver,我已经能够在Firefox无头模式下运行selenium测试用例,但是在截图时,截图不是网页(在测试用例中测试的网页),而是背景截图(如当前显示的窗口(例如运行测试用例的eclipse IDE)) 截图功能 File screenShotFolder = new File("Screenshots"); WebDriver driver = getDriver(); try { if (!screenShotFolder.exists()

我已经能够在Firefox无头模式下运行selenium测试用例,但是在截图时,截图不是网页(在测试用例中测试的网页),而是背景截图(如当前显示的窗口(例如运行测试用例的eclipse IDE))

截图功能

File screenShotFolder = new File("Screenshots");
        WebDriver driver = getDriver();
        try {
            if (!screenShotFolder.exists() && !screenShotFolder.mkdir()) {
                getLog().error(
                        "Cannot create a new file in the intended location. "
                                + "" + screenShotFolder.getAbsolutePath());
            }
            File scrFile =
                    ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            String filePath =
                    screenShotFolder.getAbsolutePath() + File.separator
                            + imageName + ".png";
            FileUtils.copyFile(scrFile, new File(filePath));

        } catch (Exception e) {
            e.printStackTrace();
        }

还有其他需要设置的“选项”或“参数”吗?

使用headless Firefox拍摄屏幕截图应该和普通驱动程序一样有效

过去,我采用了以下方法:

public static String makeScreenshot() {
    String fileName = System.currentTimeMillis() + "Test";
    File screenshot = Driver.driver.get().getScreenshotAs(OutputType.FILE);
    File outputFile = new File("LoggerScreenshots/" + fileName + ".png");
    System.out.println(outputFile.getAbsolutePath());
    try {
        FileUtils.copyFile(screenshot, outputFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return outputFile.getName();
}
并在测试执行失败时调用它:


以下是您仍然可以使用的方法

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument( "--headless" )
# options.add_argument( "--screenshot test.jpg http://google.com/" )
driver = webdriver.Firefox( firefox_options=options )
driver.get('http://google.com/')
driver.save_screenshot('test.png')
print driver.title
print driver.current_url
driver.quit()
sys.exit()

我发布的功能工作正常,问题是另一个功能正在拍摄背景截图并将其添加到最终报告中。