package rjcs; import java.io.File; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.image.BufferedImage; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.firefox.FirefoxDriver; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.imageio.ImageIO; public class zmjsj { public static void main(String[] args) { System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); //设置火狐的安装路径,防止系统找不到 FirefoxDriver driver = new FirefoxDriver(); //初始化FireFox浏览器实例,并打开浏览器 try { driver.manage().window().maximize(); //最大化窗口 Thread.sleep(3000); driver.manage().window().maximize(); //最大化窗口 Thread.sleep(3000); driver.get("https://www.baidu.com/"); Thread.sleep(3000); DateFormat dateformat= new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); //创建一个data format对象 Date date = new Date(); //利用Date()获取当前时间 String date1 = dateformat.format(date); //格式化时间,并用String对象存储 System.out.println(date1); //打印格式化时间到控制台 BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); ImageIO.write(image, "png", new File("C:\\Users\\del\\Desktop",date1+".png")); Thread.sleep(5000); }catch (Exception e) { e.printStackTrace(); }finally { driver.quit(); } } }