怎么在Java中利用selenium实现截图操作

发布时间:2021-04-19 18:11:40 作者:Leah
来源:亿速云 阅读:258

怎么在Java中利用selenium实现截图操作?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Java的特点有哪些

Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

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

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

怎么在Java中利用selenium实现截图操作

当然也是可以设置保存到其他目录下: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;
	}
	
	
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. selenium如何实现窗口截图
  2. 如何实现Python Selenium截图功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java selenium

上一篇:怎么在vue2 中使用vuedraggable组件实现一个拖动排序功能

下一篇:怎么在微信小程序中实现一个悬浮窗功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》