Selenium webdriver截图

Posted 钱_进

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium webdriver截图相关的知识,希望对你有一定的参考价值。

java语言,进行截图的代码如下:

public void screenshot(WebDriver driver){
File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);//截图代码
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");//设置日期格式
//将截图剪切到指定路径
try{
FileUtils.moveFile(screenShotFile, new File("test-output/snapshot/" + df.format(new Date())+ ".jpg"));
}catch(IOException e){
System.out.println("can‘t save screenshot");
}
}

 

其中关键的代码是

File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE)

  

以上是关于Selenium webdriver截图的主要内容,如果未能解决你的问题,请参考以下文章