例: 打开百度首页 ,进行截图
01 |
packagecom.example.tests; |
03 |
importorg.apache.commons.io.FileUtils; |
05 |
importorg.openqa.selenium.*; |
06 |
importorg.openqa.selenium.ie.InternetExplorerDriver; |
07 |
public classSelenium2 { |
09 |
public voidtestTakesScreenshot() { |
10 |
WebDriver driver = newInternetExplorerDriver(); |
11 |
driver.get("http://www.baidu.com"); |
13 |
File srcFile = ((TakesScreenshot)driver). |
14 |
getScreenshotAs(OutputType.FILE); |
16 |
(srcFile,newFile("d:\\screenshot.png")); |
17 |
} catch(Exception e) { |
TakesScreenshot接口提供了getScreenshotAs()方法来捕捉屏幕。上面的例子中,我们指定了OutputType.FILE作为参数传递给getScreenshoAs()方法,告诉它将截取的屏幕以文件形式返回。
如果使用的是RemoteWebDriver() ,则方法应该如下
首先启动selenium java -jar selenium-server-standalone-2.25.0.jar
01 |
packagecom.example.tests; |
03 |
importjava.io.IOException; |
04 |
importjava.net.MalformedURLException; |
06 |
importorg.apache.commons.io.FileUtils; |
08 |
importorg.openqa.selenium.*; |
09 |
importorg.openqa.selenium.remote.*; |
10 |
public classSelenium2 { |
12 |
public voidtestRemoteWebDriverScreenShot() { |
14 |
DesiredCapabilities capability = DesiredCapabilities.internetExplorer(); |
15 |
WebDriver driver = null; |
17 |
driver = newRemoteWebDriver( //我使用localhost来测试 |
18 |
newURL("http://localhost:4444/wd/hub"), capability); |
19 |
} catch(MalformedURLException e) { |
22 |
driver.get("http://www.sina.com.cn"); |
24 |
driver = newAugmenter().augment(driver); |
26 |
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); |
28 |
FileUtils.copyFile(scrFile, newFile("D:\\screenshot.png")); |
29 |
} catch(IOException e) { |
- JavaBeginnersTutorial 中文系列教程·翻译完成
原文:JavaBeginnersTutorial 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. 在线阅读 ApacheCN 学习资源 目录 ...
- Selenium WebDriver屏幕截图(C#版)
Selenium WebDriver屏幕截图(C#版)http://www.automationqa.com/forum.php?mod=viewthread&tid=3595&fro ...
- webdriver高级应用- 测试过程中发生异常或断言失败时进行屏幕截图
封装了三个类来实现这个功能: 1.DataUtil.py 用于获取当前的日期以及时间,用于生成保存截图文件的目录名,代码如下: #encoding=utf-8 import time from dat ...
- webdriver屏幕截图(python)
webdriver对当前页面进行截图,截取的是当前页面的全图,不论页面有多长,有两种截图方法 1.get_screenshot_as_file(XXX) 2.save_screenshot(XXX) ...
- 5.6 WebDriver API实例讲解(31-35)
31.判断页面元素是否存在 public static void testElementExist(){ driver.get("http://www.sogou.com"); t ...
- <译>Selenium Python Bindings 6 - WebDriver API
本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...
- WebDriver高级应用实例(7)
7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...
- Python实现屏幕截图的两种方式
Python实现屏幕截图的两种方式 使用windows API 使用PIL中的ImageGrab模块 下面对两者的特点和用法进行详细解释. 一.Python调用windows API实现屏幕截图 好处 ...
- Python+Selenium笔记(十六)屏幕截图
(一) 方法 方法 简单说明 save_screenshot(filename) 获取当前屏幕截图并保存为指定文件 filename:路径/文件名 get_screenshot_as_base64 ...
随机推荐
- python--随机函数(random,uniform,randint,randrange,shuffle,sample)
random() random()方法:返回随机生成的一个实数,它在[0,1)范围内 运用random()方法的语法: import random #random()方法不能直接访问,需要导入rand ...
- robotframework代码定位感悟
robotframework代码定位感悟: 在做自动化的时候,有时候还是要考虑真实的操作,考虑人为反应,网络,页面渲染,服务器处理请求的过程及数据是否真实到达响应的位置, 不能一味的按照程序的速度 去 ...
- sync命令
sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘,以提高系统的运行效率.sync命令则 ...
- php自动运行
<?php ignore_user_abort(); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行. set_time_limit(0); //执行时间为无限制,php默认 ...
- 杭电OJ分类
基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1 ...
- SharedPreferences的工具类
import android.content.Context; import android.content.SharedPreferences; import android.content.Sha ...
- 面向对象的特性-为String类型的变量扩展一个replaceAll()函数
———————————————————————————— <script type="text/javascript"> //按钮 ...
- CentOS,Ubuntu,Gentoo,Freebsd,RedHat,Debian的区别及选择
Linux最早由Linus Benedict Torvalds在1991年开始编写.在这之前,Richard Stallman创建了Free Software Foundation(FSF)组织以及G ...
- hdu_1950_Bridging signals(LIS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1950 题意:实际就是求最长递增子序列 题解:有两种解法,一种是利用二分,一种是用线段树 这个是这题的二 ...
- Entity Framework技巧系列之七 - Tip 26 – 28
提示26. 怎样避免使用不完整(Stub)实体进行数据库查询 什么是不完整(Stub)实体? 不完整实体是一个部分填充实体,用于替代真实的对象. 例如: 1 Category c = new Cate ...