例: 打开百度首页 ,进行截图
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 ...
随机推荐
- 【Linux】zookeeper构造伪集群
1.在一台机器装安装3个zk server,构建伪集群模式安装步骤如下:1.下载zookeeper,下载地址:http://mirror.bit.edu.cn/apache/zookeeper/zoo ...
- Android Lights
Android Lights 很多Android手机上都配有LED灯,手机在充电.新来短信等时候都会有相应的指示灯提示. Android系统之中,一共定义了8个逻辑灯,包含:背光,键盘灯,按键灯,充电 ...
- mahout与nosql的两幅经典图形
- MFC中实现定时执行与提醒功能(自编代码)
具体实现代码如下:添加一个计时器:SetTimer(1,1000,NULL); 下面仅列举核心代码,详细步聚不作说明,效果如下所示: void CShowTimer::OnTimer(UINT_PTR ...
- [转]Publishing and Running ASP.NET Core Applications with IIS
本文转自:https://weblog.west-wind.com/posts/2016/Jun/06/Publishing-and-Running-ASPNET-Core-Applications- ...
- H5的新应用-获取用户当前的地理坐标
------------------------------ <script type="text/javascript"> ...
- JavaScript在智能手机上的应用-使用手机GPS定位用户所在城市
---------------------------- <script type="text/javascript" language="javascript&q ...
- GlusterFS常用命令小结
# /etc/init.d/glusterd start # /etc/init.d/glusterd stop # /etc/init.d/glusterd status 2. 开机自动 ...
- C# Memcached缓存
net/c#分布式缓存系统Memcached简介与实践--非常好 缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是 ...
- Cake
Cake Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...