Java selenium截图操作的实现

方法一:Selenium中截图类TakeScreenshout,这个类主要是获取浏览器窗体内的内容,不包括浏览器的菜单和桌面的任务栏区域,我们用百度首页来截图,看看截图效果。

FileUtils.copyFile(srcFile, new File("屏幕截图", time + ".png"));“屏幕截图”是我们自己创建的文件夹用来存放截图文件,此文件夹在project(工程)的更目录

当然也是可以设置保存到其他目录下:FileUtils.copyFile(srcFile, new File("D:\\资料图片", time + ".png"));

示例代码如下:

package com.sandy;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScreenShot {

	private static WebDriver driver;
	public static void main(String[] args) throws Exception {

		System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.get("http://www.baidu.com");
		driver.manage().window().maximize();

		/**
		 * 截屏操作
		 * 图片已当前时间命名
		 */
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); //转换时间格式
		String time = dateFormat.format(Calendar.getInstance().getTime()); //获取当前时间
		File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //执行屏幕截取
		FileUtils.copyFile(srcFile, new File("屏幕截图", time + ".png")); //利用FileUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件;"屏幕截图"即时保存截图的文件夹
		Thread.sleep(2000);
		driver.quit();

	}

}

方法二:Robot截屏

示例代码:(示例中的图片是保存再该工程的根目录下)

package com.sandy;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.internal.WrapsDriver;

public class ScreenShot {

	private static WebDriver driver;
	public static void main(String[] args) throws Exception {

		System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.get("http://www.baidu.com");
		driver.manage().window().maximize();
		robotSnapshot();

		Thread.sleep(2000);
		driver.quit();

	}

	/**
	 * 截屏方法二、Robot实现截屏
	 * @throws Exception
	 */
	public static void robotSnapshot() throws Exception {
		//调用截图方法
		BufferedImage img = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		ImageIO.write(img, "png", new File("robot_screen01.png"));
	}

方法三:在测试的过程中,有时候不需要截取整个屏幕,只需要截取某个元素(或者目标区域)的图片

示例代码:

package com.sandy;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.internal.WrapsDriver;

public class ScreenShot {

	private static WebDriver driver;
	public static void main(String[] args) throws Exception {

		System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.get("http://www.baidu.com");
		driver.manage().window().maximize();

		WebElement element = driver.findElement(By.id("su"));
		elementSnapshot(element);
		//System.currentTimeMillis()、Calendar.getInstance().getTimeInMillis()获取时间戳的方法
		FileUtils.copyFile(elementSnapshot(element), new File("屏幕截图", System.currentTimeMillis()+".png"));
		Thread.sleep(2000);
		driver.quit();

	}

	/**
	 * 部分截图(元素截图)
	 * 有时候需要元素的截图,不需要整个截图
	 * @throws Exception
	 */
	public static File elementSnapshot(WebElement element) throws Exception {
		//创建全屏截图
		WrapsDriver wrapsDriver = (WrapsDriver)element;
		File screen = ((TakesScreenshot)wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
		BufferedImage image = ImageIO.read(screen);
		//获取元素的高度、宽度
		int width = element.getSize().getWidth();
		int height = element.getSize().getHeight();

		//创建一个矩形使用上面的高度,和宽度
		Rectangle rect = new Rectangle(width, height);
		//元素坐标
		Point p = element.getLocation();
		BufferedImage img = image.getSubimage(p.getX(), p.getY(), rect.width, rect.height);
		ImageIO.write(img, "png", screen);
		return screen;
	}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Selenium Webdriver实现截图功能的示例

    前几天在研究中自动化的时候突发奇想,想着能不能来截个图,以便之后查看,实现的方法其实也不难,毕竟selenium webdriver已经提供了截图额功能,TakesScreenshot接口函数(英文意思就是获取屏幕截图takes-screenshot). 废话不多说了,直接上代码 package com.wch; import java.io.File; import java.io.IOException; import org.junit.After; import org.junit.Be

  • Python实现的网页截图功能【PyQt4与selenium组件】

    本文实例讲述了Python实现的网页截图功能.分享给大家供大家参考,具体如下: 方法一.使用PyQt4的QtWebKit组件 #!/usr/bin/env python # -*- coding: UTF-8 -*- import sys import os.path from PyQt4 import QtGui,QtCore,QtWebKit class PageShotter(QtGui.QWidget): def __init__(self,url,filename,parent=Non

  • Python+selenium实现截图图片并保存截取的图片

    这篇文章介绍如何利用Selenium的方法进行截图,在测试过程中,是有必要截图,特别是遇到错误的时候进行截图.在selenium for Python中主要有三个截图方法,我们挑选其中最常用的一种. 截图技能对于测试人员来说应该是较为重要的一个技能. 在自动化测试中,截图可以帮助我们直观的定位错误.记录测试步骤. 记得以前在给某跨国银行做自动化项目的时候,某银的PM要求我们自动化测试的每一步至少需要1个截图,以证明每个功能都被自动化测试给覆盖过,在这种情况下截图就成了证明自动化测试有效性的重要手

  • Python中使用 Selenium 实现网页截图实例

    Selenium 是一个可以让浏览器自动化地执行一系列任务的工具,常用于自动化测试.不过,也可以用来给网页截图.目前,它支持 Java.C#.Ruby 以及 Python 四种客户端语言.如果你使用 Python,则只需要在命令行里输入"sudo easy_install selenium"并回车,即可安装 selenium 的 Python 版本的客户端支持. 以 Python 为例,我们可以使用下面的脚本来给指定页面(比如我们首页)截图: # -*- coding: utf-8 -

  • 关于Selenium的UI自动化测试屏幕截图功能实例代码

    UI自动化测试执行过程中,当遇到检查失败的情况,往往会发现打印的log并不能有效地帮助我们定位问题.我们需要失败时刻的屏幕截图来重现当时的失败场景,进而排查出错原因. 基于这种需求可以使用Selenium的屏幕截图功能. 实现代码如下: import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.time.DateU

  • selenium+python截图不成功的解决方法

    selenium+python,使用webdriver的截图函数get_screenshot_as_file()截图,代码如下: from selenium import webdriver from time import sleep driver=webdriver.Chrome() driver.get("https://www.baidu.com") driver.find_element_by_id("kw").send_keys("seleni

  • Java selenium截图操作的实现

    方法一:Selenium中截图类TakeScreenshout,这个类主要是获取浏览器窗体内的内容,不包括浏览器的菜单和桌面的任务栏区域,我们用百度首页来截图,看看截图效果. FileUtils.copyFile(srcFile, new File("屏幕截图", time + ".png"));"屏幕截图"是我们自己创建的文件夹用来存放截图文件,此文件夹在project(工程)的更目录 : 当然也是可以设置保存到其他目录下:FileUtils.

  • java selenium 操作浏览器实例

    本篇文章介绍selenium 操作浏览器 阅读目录 浏览器最大化 前进,后退, 刷新 截图操作 模拟鼠标操作 杀掉Windows浏览器进程 浏览器最大化 前进,后退, 刷新 public static void testBrowser(WebDriver driver) throws Exception { driver.get("http://www.cnblogs.com/tankxiao"); Thread.sleep(5000); // 浏览器最大化 driver.manage

  • java selenium操作弹出对话框示例讲解

    Web 开发人员通常需要利用JavaScript弹出对话框来给用户一些信息提示, 包括以下几种类型 阅读目录 对话框类型 测试页面 Selenium 操作对话框的代码 对话框类型 1.  警告框: 用于提示用户相关信息的验证结果, 错误或警告等 2. 提示框: 用于提示用户在当前对话框中输入数据,一般需要用户单击取消或者确认按钮 3. 确认框: 用于提示用户确认或者取消某个操作,一般需要用户单击取消或者确认按钮 测试页面 用如下页面为例进行讲解, 包括了警告框,提示框,确认框 http://si

  • java selenium 常见web UI 元素操作及API使用

    本篇介绍我们如何利用selenium 来操作各种页面元素 阅读目录 链接(link) 输入框 textbox 按钮(Button) 下拉选择框(Select) 单选按钮(Radio Button) 多选框 check box 链接(link) <div> <p>链接 link</p> <a href="www.cnblogs.com/tankxiao">小坦克</a> </div> 链接的操作 // 找到链接元素

  • java selenium 操作弹出窗口示例代码

    selenium 中如何处理弹出窗口 阅读目录 原理 测试页面的HTML Java 代码 原理 在代码里, 通过         Set<String> allWindowsId = driver.getWindowHandles(); 来获取到所有弹出浏览器的句柄,   然后遍历,  使用swithcto.window(newwindow_handle)方法. 就可以定位到新的窗口 测试页面的HTML <html> <head> <title>常见web

  • java selenium XPath 定位实现方法

    xpath 的定位方法, 非常强大.  使用这种方法几乎可以定位到页面上的任意元素. 阅读目录 什么是xpath xpath定位的缺点 testXpath.html 代码如下 绝对路径定位方式 使用浏览器调试工具,可以直接获取xpath语句 绝对路径的缺点 绝对路径和相对路径的区别 相对路径定位方式 使用索引号定位 使用页面属性定位 模糊定位starts-with关键字 模糊定位contains关键字 text() 函数 文本定位 什么是xpath xpath 是XML Path的简称, 由于H

  • java selenium元素定位大全

    页面元素定位是自动化中最重要的事情, selenium Webdriver 提供了很多种元素定位的方法.  测试人员应该熟练掌握各种定位方法. 使用最简单,最稳定的定位方法. 阅读目录 自动化测试步骤 定位方法大全 如何定位 通过ID查找元素: By.id() 通过Name查找元素:By.name() 通过TagName查找元素: By.tagName() 通过ClassName 查找元素 By.className 通过LinkText查找元素 By.linkText(); 通过PartialL

  • java selenium智能等待页面加载完成示例代码

    java selenium  智能等待页面加载完成 我们经常会碰到用selenium操作页面上某个元素的时候, 需要等待页面加载完成后, 才能操作.  否则页面上的元素不存在,会抛出异常. 或者碰到AJAX异步加载,我们需要等待元素加载完成后, 才能操作 selenium 中提供了非常简单,智能的方法,来判断元素是否存在. 阅读目录 实例要求 隐式等待 显式等待 实例要求 实例:set_timeout.html 下面的html 代码,  点击click 按钮5秒后, 页面上会出现一个红色的div

  • java selenium Selenium IDE介绍及用法

    Selenium IDE 是Firefox 浏览器的一个插件, 它会记录你对Firefox的操作,并且可以回放它的操作. 用法简单,不过我觉得这个没多大的用处 阅读目录 Selenium IDE 介绍 Selenium IDE 的作用 Selenium IDE 在线安装方法一 Selenium IDE 安装方法二 (本地安装) Selenium IDE  安装不上的原因. 打开Selenium Selenium IDE 的使用方法 界面介绍 导出脚本 Selenium IDE 介绍 Seleni

  • java+selenium爬取图片签名的方法

    本文实例为大家分享了java+selenium爬取图片签名的具体实现方法,供大家参考,具体内容如下 学习记录: 1.注意 对应的版本非常重要,使用selenium得下载与游览器版本相对应的插件,有火狐和谷歌我用的谷歌,贴下谷歌driver的插件 查看谷歌版本: 2.插件存放路径 3.获取签名图片存放路径 4.Controller代码如下 @ResponseBody @RequestMapping(value = "signatureGenerationv") public String

随机推荐