selenium webdriver - 截图

selenium2集成了TakeScreenshot插件,可以支持浏览器截图功能

File screenShotFile = ((TakesScreenshot) dr).getScreenshotAs(OutputType.FILE ); 
FileUtils.copyFile (screenShotFile, new File("D:/JIETU.PNG"));

 

完成代码:

selenium webdriver - 截图
 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 import org.apache.commons.io.FileUtils;
 5 import org.openqa.selenium.OutputType;
 6 import org.openqa.selenium.TakesScreenshot;
 7 import org.openqa.selenium.WebDriver;
 8 import org.openqa.selenium.firefox.FirefoxDriver;
 9 public class ShotScreen {
10 
11     /**
12      * 截图
13      */
14     public static void main(String[] args) throws IOException, InterruptedException {
15         
16         WebDriver driver = new FirefoxDriver();
17         driver.get("http://www.baidu.com");
18                 
19         //这里等待页面加载完成
20         Thread.sleep(5000);
21         
22         captureScreen(driver, "D:/jietu/", "test1", "png");
23         
24         driver.close();
25     }
26     /*
27      * 截图
28      * @param dr driver实例
29      * @param directory 截图路径,如"D:/jietu"
30      * @param captureName 截图文件名
31      * @param format    截图文件格式
32      */
33     
34     public static void captureScreen(WebDriver dr, String directory,String captureName, String format){ 
35         //获取图片
36         File screenShotFile = ((TakesScreenshot) dr).getScreenshotAs(OutputType.FILE ); 
37         
38         try {
39         //保存文件到目录
40             if (directory.endsWith("/")) {
41                 FileUtils.copyFile (screenShotFile, new File(directory + captureName +"."+ format));
42             }else {
43                 FileUtils.copyFile (screenShotFile, new File(directory +"/"+ captureName +"."+ format)); 
44             }
45          } catch (IOException e)
46          { 
47              e.printStackTrace(); 
48          } 
49      }
50 }
selenium webdriver - 截图

selenium webdriver - 截图

上一篇:ie6 js报错汇总


下一篇:selenium webdriver - 浏览器最大化