packagerjcs;importjava.text.DateFormat;importjava.text.SimpleDateFormat;import java.util.*;import java.awt.*;importjava.awt.datatransfer.StringSelection;importjava.awt.event.KeyEvent;importjava.io.File;importorg.apache.commons.io.FileUtils;import org.openqa.selenium.support.*;importorg.openqa.selenium.By;importorg.openqa.selenium.JavascriptExecutor;importorg.openqa.selenium.Keys;importorg.openqa.selenium.OutputType;importorg.openqa.selenium.TakesScreenshot;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.firefox.FirefoxDriver;importorg.openqa.selenium.firefox.FirefoxProfile;importorg.openqa.selenium.interactions.Actions;importorg.openqa.selenium.support.ui.Select;public classxinkaishi
{public static voidmain(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();//最大化窗口
driver.navigate().to("https://www.baidu.com");
Thread.sleep(5000);
DateFormat dateformat1= new SimpleDateFormat("yyyy-MM-dd"); //创建一个data format对象
Date date1 = new Date(); //利用Date()获取当前时间
String datex = dateformat1.format(date1); //格式化时间,并用String对象存储
System.out.println(datex); //打印格式化时间到控制台
DateFormat dateformat2= new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); //创建一个data format对象
Date date2 = new Date(); //利用Date()获取当前时间
String datey = dateformat2.format(date2); //格式化时间,并用String对象存储
System.out.println(datey); //打印格式化时间到控制台
DateFormat dateformat3= new SimpleDateFormat("HH-mm-ss"); //创建一个data format对象
Date date3 = new Date(); //利用Date()获取当前时间
String dateu = dateformat3.format(date3); //格式化时间,并用String对象存储
System.out.println(dateu); //打印格式化时间到控制台
File src02= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //调用截图方法
FileUtils.copyFile(src02, new File("C:\\Users\\del\\Desktop\\"+datex, dateu + ".png"));
Thread.sleep(5000);
}catch(Exception e)
{
e.printStackTrace();
}finally{
driver.quit();
}
}
}