例: 打开百度首页 ,进行截图

01 packagecom.example.tests; 
02 importjava.io.File; 
03 importorg.apache.commons.io.FileUtils; 
04 importorg.junit.*; 
05 importorg.openqa.selenium.*; 
06 importorg.openqa.selenium.ie.InternetExplorerDriver; 
07 public classSelenium2 { 
08     @Test 
09     public voidtestTakesScreenshot() { 
10         WebDriver driver = newInternetExplorerDriver(); 
11         driver.get("http://www.baidu.com"); 
12         try
13             File srcFile = ((TakesScreenshot)driver). 
14                     getScreenshotAs(OutputType.FILE); 
15             FileUtils.copyFile 
16             (srcFile,newFile("d:\\screenshot.png")); 
17         catch(Exception e) { 
18             e.printStackTrace(); 
19         }  
20           driver.close(); 
21         
22 }

TakesScreenshot接口提供了getScreenshotAs()方法来捕捉屏幕。上面的例子中,我们指定了OutputType.FILE作为参数传递给getScreenshoAs()方法,告诉它将截取的屏幕以文件形式返回。

如果使用的是RemoteWebDriver() ,则方法应该如下

首先启动selenium java -jar selenium-server-standalone-2.25.0.jar

01 packagecom.example.tests; 
02 importjava.io.File; 
03 importjava.io.IOException; 
04 importjava.net.MalformedURLException; 
05 importjava.net.URL; 
06 importorg.apache.commons.io.FileUtils; 
07 importorg.junit.*; 
08 importorg.openqa.selenium.*; 
09 importorg.openqa.selenium.remote.*; 
10 public classSelenium2 { 
11     @Test 
12     public voidtestRemoteWebDriverScreenShot() { 
13         //指定使用的浏览器 
14         DesiredCapabilities capability = DesiredCapabilities.internetExplorer(); 
15         WebDriver driver = null
16         try
17             driver = newRemoteWebDriver( //我使用localhost来测试 
18                     newURL("http://localhost:4444/wd/hub"), capability); 
19         catch(MalformedURLException e) { 
20             e.printStackTrace(); 
21         
22         driver.get("http://www.sina.com.cn"); 
23         //对远程系统进行截图 
24         driver = newAugmenter().augment(driver);  
25         File scrFile =   
26           ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
27         try
28             FileUtils.copyFile(scrFile, newFile("D:\\screenshot.png")); 
29         catch(IOException e) { 
30             e.printStackTrace(); 
31         
32     
33 }

转:WebDriver进行屏幕截图的更多相关文章

  1. JavaBeginnersTutorial 中文系列教程·翻译完成

    原文:JavaBeginnersTutorial 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. 在线阅读 ApacheCN 学习资源 目录 ...

  2. Selenium WebDriver屏幕截图(C#版)

    Selenium WebDriver屏幕截图(C#版)http://www.automationqa.com/forum.php?mod=viewthread&tid=3595&fro ...

  3. webdriver高级应用- 测试过程中发生异常或断言失败时进行屏幕截图

    封装了三个类来实现这个功能: 1.DataUtil.py 用于获取当前的日期以及时间,用于生成保存截图文件的目录名,代码如下: #encoding=utf-8 import time from dat ...

  4. webdriver屏幕截图(python)

    webdriver对当前页面进行截图,截取的是当前页面的全图,不论页面有多长,有两种截图方法 1.get_screenshot_as_file(XXX) 2.save_screenshot(XXX) ...

  5. 5.6 WebDriver API实例讲解(31-35)

    31.判断页面元素是否存在 public static void testElementExist(){ driver.get("http://www.sogou.com"); t ...

  6. <译>Selenium Python Bindings 6 - WebDriver API

    本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...

  7. WebDriver高级应用实例(7)

    7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...

  8. Python实现屏幕截图的两种方式

    Python实现屏幕截图的两种方式 使用windows API 使用PIL中的ImageGrab模块 下面对两者的特点和用法进行详细解释. 一.Python调用windows API实现屏幕截图 好处 ...

  9. Python+Selenium笔记(十六)屏幕截图

    (一) 方法 方法 简单说明 save_screenshot(filename)   获取当前屏幕截图并保存为指定文件 filename:路径/文件名 get_screenshot_as_base64 ...

随机推荐

  1. mvc路由参数注解

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //过滤掉禁止访问的路由 routes.MapRoute( name: &quo ...

  2. ECOS-Ecstore 后台管理地址修改

    ECStore默认出厂的后台管理地址是: http://域名/index.php/shopadmin http://域名/shopadmin [配置过rewrite后,并开启伪静态] 如果想要更个性的 ...

  3. startssl证书firefox支持配置

    解决Firefox不信任StartSSL证书问题 wget http://cert.startssl.com/certs/ca.pem wget http://cert.startssl.com/ce ...

  4. 网页 HTML表单

    今天,我首先先学习了图片热点和在原来页面嵌入其他页面. 图片热点:<img src="" usemap="#ditu"/> <map name ...

  5. POJ1613 147/思维题

    题目链接[https://www.oj.swust.edu.cn/problem/show/1613] 题意:输出第K小的由1.4.7数字组成的数字. 解题过程:1.4.7.11.14.17.41.4 ...

  6. mongodb type it for more

    当使用MongoChef Core 链接mongodb的时候 ,需要查看更多的数据时候,系统提示  type it for more 可以设置系统参数 DBQuery.shellBatchSize = ...

  7. ubuntu server 时区设置问题解决

    1.当执行此命令的时候 ntpdate us.pool.ntp.org 出现一下错误提示 name server cannot be used: Temporary failure in name r ...

  8. 初探OpenGL(一)

    OPenGL ES 1.X 面向功能固定的硬件所涉及并提供加速支持,图形质量以及性能标准. OpenGL ES2.X则提供包括着色器技术在内的全编程3D图形算法.----硬件要求比较高. OpenGL ...

  9. HDU 5543 Pick The Sticks

    背包变形.与普通的背包问题不同的是:允许有两个物品可以花费减半. 因此加一维即可,dp[i][j][k]表示前i个物品,有j个花费减半了,总花费为k的情况下的最优解. #pragma comment( ...

  10. HDU 3081 Marriage Match II

    二分图的最大匹配+并查集 每次匹配完之后,删除当前匹配到的边. #include<cstdio> #include<cstring> #include<cmath> ...