Java selenium截图操作的实现

 更新时间:2019年08月05日 10:34:45   作者:lykion_881210  
这篇文章主要介绍了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;
	}
	
	
}

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

相关文章

  • 基于JavaScript动态规划编写一个益智小游戏

    基于JavaScript动态规划编写一个益智小游戏

    最近在学习动态规划相关的知识,所以本文将利用动态规划编写一个简单的益智小游戏,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2023-06-06
  • Java之单例模式实现方案详解

    Java之单例模式实现方案详解

    这篇文章主要介绍了Java之单例模式实现方案详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • 一次 Java 内存泄漏的排查解决过程详解

    一次 Java 内存泄漏的排查解决过程详解

    这篇文章主要介绍了一次 Java 内存泄漏的排查过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 详解mybatis中的if-else的嵌套使用

    详解mybatis中的if-else的嵌套使用

    本文主要介绍了mybatis中的if-else的嵌套使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • SpringBoot实用小技巧之如何动态设置日志级别

    SpringBoot实用小技巧之如何动态设置日志级别

    这篇文章主要给大家介绍了关于SpringBoot实用小技巧之如何动态设置日志级别的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用SpringBoot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • Spring MVC整合FreeMarker的示例

    Spring MVC整合FreeMarker的示例

    这篇文章主要介绍了Spring MVC整合FreeMarker的示例,帮助大家更好的理解和使用Spring MVC,感兴趣的朋友可以了解下
    2020-12-12
  • Spring七大事务传递机制深入分析实现原理

    Spring七大事务传递机制深入分析实现原理

    实际项目开发中,如果涉及到多张表操作时,为了保证业务数据的一致性,大家一般都会采用事务机制,好多小伙伴可能只是简单了解一下,遇到事务失效的情况,便会无从下手,下面这篇文章主要给大家介绍了关于Spring事务传递机制的相关资料,需要的朋友可以参考下
    2023-03-03
  • Spring事件监听基本原理与使用详解

    Spring事件监听基本原理与使用详解

    这篇文章主要介绍了Spring事件监听基本原理与使用详解,Spring的事件监听机制和发布订阅机制是很相似的:发布了一个事件后,监听该类型事件的所有监听器会触发相应的处理逻辑,需要的朋友可以参考下
    2024-01-01
  • ActiveMQ结合Spring收发消息的示例代码

    ActiveMQ结合Spring收发消息的示例代码

    这篇文章主要介绍了ActiveMQ结合Spring收发消息的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • 关于Spring中的三级缓存解析

    关于Spring中的三级缓存解析

    这篇文章主要介绍了关于Spring中的三级缓存,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08

最新评论