Selenium提供了截图的功能,其接口是 TakesScreenshots。
该功能可以在异常或者错误发生的时候将屏幕截取并保存起来,供后续分析和调试所用;这个TakesScrernshot类主要是来获取浏览器窗体的内容,不包括浏览器的菜单或者桌面等。
下面我们以百度新闻的首页为例来实现截图的功能
首先我们先在我们的src源文件夹下面新建一个文件夹叫做Screenshots用来存放我们的截图文件
package com.yumeihu.D1;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ScreenShoot {
public static void main(String[] args) throws Exception {
WebDriver driver = new F