selenium web driver 实现截图功能
在验证某些关键步骤时,需要截个图来记录一下当时的情况
Webdriver截图时,需要引入
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
截图方法
public static void snapshot(TakesScreenshot drivername, String filename)
{
// this method will take screen shot ,require two parameters ,one is driver name, another is file name String currentPath = System.getProperty("user.dir"); //get current work folder
System.out.println(currentPath);
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
System.out.println("save snapshot path is:"+currentPath+"/"+filename);
FileUtils.copyFile(scrFile, new File(currentPath+"\\"+filename));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
}
finally
{ System.out.println("screen shot finished");
}
}
以下任务:
1.使用selenium打开百度,截图;
2.输入selenium关键字,截图;
3.搜索 并打开 selenium的百度百科,截图;
具体代码如下:
package baidu; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; public class selenium { public static void snapshot(TakesScreenshot drivername, String filename)
{
// this method will take screen shot ,require two parameters ,one is driver name, another is file name File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
System.out.println("save snapshot path is:E:/"+filename);
FileUtils.copyFile(scrFile, new File("E:\\"+filename));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
}
finally
{
System.out.println("screen shot finished");
}
} public static void main (String [] args) throws InterruptedException
{ String URL="http://www.baidu.com";
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(URL);
//max size the browser
driver.manage().window().maximize();
/*
Navigation navigation = driver.navigate();
navigation.to(URL);*/
Thread.sleep(2000);
snapshot((TakesScreenshot)driver,"open_baidu.png");
//WebElement reg=driver.findElement(By.name("tj_reg"));
//reg.click();
// WebElement keyWord = driver.findElement(By.id("kw1")); //find the element
WebElement keyWord = driver.findElement(By.xpath("//input[@id='kw1']"));
keyWord.clear();
//send key words
keyWord.sendKeys("Selenium");
Thread.sleep(3000);
snapshot((TakesScreenshot)driver,"input_keyWord.png"); WebElement submit = driver.findElement(By.id("su1")); System.out.println(submit.getLocation());
submit.click();
//System.out.println(driver.getWindowHandle());
Thread.sleep(5000); // System.out.println(driver.getPageSource()); String pageSource=driver.getPageSource();
// System.out.println(pageSource);
//WebElement link =driver.findElement(By.xpath(SELENIUM_LINK));
WebElement link =driver.findElement(By.xpath("//*[@id=\"1\"]/h3/a")); //*[@id="1"]/h3/a
link.click();
Thread.sleep(5000);
driver.switchTo().window(driver.getWindowHandles().toArray(new String[0])[1]); //get page title
System.out.println(driver.getTitle());
Thread.sleep(5000);
// navigation.back();
snapshot((TakesScreenshot)driver,"open_bake.png");
System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl()); driver.quit(); } }
在百度搜索结果中,拿到你想要的elements,可以使用浏览器的查看元素,通过xpath方法获取
运行此代码后截图效果如下:
console输出:
Starting ChromeDriver (v2.9.248315) on port 33834
save snapshot path is:E:/open_baidu.png
screen shot finished
save snapshot path is:E:/input_keyWord.png
screen shot finished
(858, 179)
Selenium_百度百科
save snapshot path is:E:/open_bake.png
screen shot finished
Selenium_百度百科
http://baike.baidu.com/subview/478050/6464537.htm?fr=aladdin
selenium web driver 实现截图功能的更多相关文章
- 25+ Useful Selenium Web driver Code Snippets For GUI Testing Automation
本文总结了使用Selenium Web driver 做页面自动化测试的一些 tips, tricks, snippets. 1. Chrome Driver 如何安装 extensions 两种方式 ...
- selenium web driver 配合使用testng
首先为eclipse添加testng插件 步骤如下:help->Install New SoftWare... 2. 添加testng链接,该链接可以在这里找到 For the Eclipse ...
- selenium web driver 使用JS修改input属性
selenium获取input时候,发现type=”hidden” 的input无法修改value,经牛人指点,可以使用js修改 首先html源文件如下,设置为text .hidden.submit ...
- selenium web driver
WebDriver 支持的浏览器 IE6-10 FireFox大部分版本 Chrome Safari Opera Andrioid 系统上的自带浏览器 IOS系统上自带浏览器 HtmlUnit的无界面 ...
- 封装selenium自动化框架中的截图功能
对selenium自带的截图功能进行封装: 以下为封装的代码,自定义一个.py文件即可,图片路径自己设置一个. #coding:utf-8 class Screen(object): ''' 封装的截 ...
- selenium截图功能
selenium自动化测试完后需要查看值观的结果,或者查操作过程中是否正确,此时需要使用自带的截图功能. 示例1: from time import sleep from selenium impor ...
- Selenium Webdriver——实现截图功能
截图方法 public static void snapshot(TakesScreenshot drivername, String filename) { // this method will ...
- Selenium Web 自动化 - 项目实战(三)
Selenium Web 自动化 - 项目实战(三) 2016-08-10 目录 1 关键字驱动概述2 框架更改总览3 框架更改详解 3.1 解析新增页面目录 3.2 解析新增测试用例目录 3. ...
- Selenium Web 自动化 - 项目实战(一)
Selenium Web 自动化 - 测试框架(一) 2016-08-05 目录 1 框架结构雏形2 把Java项目转变成Maven项目3 加入TestNG配置文件4 Eclipse编码修改5 编写代 ...
随机推荐
- 八月22日,django知识点总结:
八月22日,知识点总结: python manage.py makemigrations python manage.py migrate unique=true是指这个字段的值在这张表里不能重复,所 ...
- php stdclass转数组
打印输出是这样 object(stdClass)[11] //object public 'xx' => string 'xxxxxx' (length=21)可用函数处理 get_object ...
- tensorflow中的基本概念
本文是在阅读官方文档后的一些个人理解. 官方文档地址:https://www.tensorflow.org/versions/r0.12/get_started/basic_usage.html#ba ...
- UVA232
这只是大概的雏形. 步骤就是:1输入网格,2给网格里的起始格编序号,3输出所有字母,前面要加序号 #include<stdio.h> #include<ctype.h> #in ...
- 做web开发和测试,修改hosts指定某个域名访问某个特定的IP后,如何使hosts立即生效的方法
本文转自SUN'S BLOG,原文地址:http://whosmall.com/post/143 hosts的配置方法: 在windows系统中,找到C:\windows\system32\drive ...
- Greenplum记录(二):估计存储容量
存储空间除了用来存储用户数据,还需要:landing backup files and data load files 空系统的存储空间:disk_size * number_of_disks 除去系 ...
- oracle 12c 加入系统服务
1修改oratab文件 vi /etc/oratab #把后台一行的N改为Y db01:/usr/oracle/app/product/11.2.0/dbhome_1:Y 2如果安装时.bash_pr ...
- Java集合之LinkedList
一.LinkedList概述 1.初识LinkedList 上一篇中讲解了ArrayList,本篇文章讲解一下LinkedList的实现. LinkedList是基于链表实现的,所以先讲解一下什么是链 ...
- 【CentOS7之防火墙命令】
1.CentOS7的发现带了很多行的指令和新的技术.并且在帮助的中文解释也增多了很多这意味着Linux在中国的发展越来越呈现普及.今天来介绍下CentOS7的新防火墙firewall. 更多的可查看: ...
- [译]C#编码约定
原文:https://msdn.microsoft.com/en-us/library/ff926074.aspx 编码约定的目的是: 创建统一格式的代码,让读者的注意力更集中在内容上面,而不是结构 ...