引入几个需要的包:
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.remote.RemoteWebDriver;
import com.google.common.io.Files;
具体代码实现
File scrFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
//driver转换成remotewebdriver, 用getScreenshotAs()截屏后返回文件对象,赋值给scrFile
try {
Files.copy(scrFile, new File("e:\\screenfile.png")); //scrFile内容复制到e盘。
} catch (IOException e) {
// 异常处理
e.printStackTrace();
}