亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java selenium截圖操作的實現(xiàn)

 更新時間:2019年08月05日 10:34:45   作者:lykion_881210  
這篇文章主要介紹了Java selenium截圖操作的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

方法一:Selenium中截圖類TakeScreenshout,這個類主要是獲取瀏覽器窗體內(nèi)的內(nèi)容,不包括瀏覽器的菜單和桌面的任務欄區(qū)域,我們用百度首頁來截圖,看看截圖效果。

FileUtils.copyFile(srcFile, new File("屏幕截圖", time + ".png"));“屏幕截圖”是我們自己創(chuàng)建的文件夾用來存放截圖文件,此文件夾在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"); //轉(zhuǎn)換時間格式
		String time = dateFormat.format(Calendar.getInstance().getTime()); //獲取當前時間
		File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //執(zhí)行屏幕截取
		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實現(xiàn)截屏
	 * @throws Exception
	 */
	public static void robotSnapshot() throws Exception {
		//調(diào)用截圖方法
		BufferedImage img = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		ImageIO.write(img, "png", new File("robot_screen01.png"));
	}

方法三:在測試的過程中,有時候不需要截取整個屏幕,只需要截取某個元素(或者目標區(qū)域)的圖片

示例代碼:

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 {
		//創(chuàng)建全屏截圖
		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();
		
		//創(chuàng)建一個矩形使用上面的高度,和寬度
		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;
	}
	
	
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Spring Security6配置方法(廢棄WebSecurityConfigurerAdapter)

    Spring Security6配置方法(廢棄WebSecurityConfigurerAdapter)

    本文主要介紹了Spring Security6配置方法(廢棄WebSecurityConfigurerAdapter),就像文章標題所說的,SpringSecurity已經(jīng)廢棄了繼承WebSecurityConfigurerAdapter的配置方式,下面就來詳細的介紹一下,感興趣的可以了解一下
    2023-12-12
  • java框架之maven是用來做什么的

    java框架之maven是用來做什么的

    這篇文章主要介紹了java之maven是用來做什么的,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • 解析Java定時任務的選型及改造問題

    解析Java定時任務的選型及改造問題

    這篇文章主要介紹了Java定時任務的選型及改造問題,本文給大家提到了Java主流三大定時任務框架優(yōu)缺點,通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-02-02
  • Java JDBC API介紹與實現(xiàn)數(shù)據(jù)庫連接池流程

    Java JDBC API介紹與實現(xiàn)數(shù)據(jù)庫連接池流程

    JDBC是指Java數(shù)據(jù)庫連接,是一種標準Java應用編程接口( JAVA API),用來連接 Java 編程語言和廣泛的數(shù)據(jù)庫。從根本上來說,JDBC 是一種規(guī)范,它提供了一套完整的接口,允許便攜式訪問到底層數(shù)據(jù)庫,本篇文章我們來了解JDBC API及數(shù)據(jù)庫連接池
    2022-12-12
  • kafka消費者kafka-console-consumer接收不到數(shù)據(jù)的解決

    kafka消費者kafka-console-consumer接收不到數(shù)據(jù)的解決

    這篇文章主要介紹了kafka消費者kafka-console-consumer接收不到數(shù)據(jù)的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Spring @Value注解失效問題解決方案

    Spring @Value注解失效問題解決方案

    這篇文章主要介紹了Spring @Value注解失效問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • 淺談java實現(xiàn)重載的方法

    淺談java實現(xiàn)重載的方法

    方法重載是讓類以統(tǒng)一的方式處理不同類型數(shù)據(jù)的一種手段。多個同名函數(shù)同時存在,具有不同的參數(shù)個數(shù)/類型。重載Overloading是一個類中多態(tài)性的一種表現(xiàn)。Java的方法重載,就是在類中可以創(chuàng)建多個方法,它們具有相同的名字,但具有不同的參數(shù)和不同的定義。
    2015-09-09
  • FastDFS分布式文件系統(tǒng)環(huán)境搭建及安裝過程解析

    FastDFS分布式文件系統(tǒng)環(huán)境搭建及安裝過程解析

    這篇文章主要介紹了FastDFS分布式文件系統(tǒng)環(huán)境搭建及安裝過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot調(diào)用第三方WebService接口的兩種方法

    SpringBoot調(diào)用第三方WebService接口的兩種方法

    本文主要介紹了SpringBoot調(diào)用第三方WebService接口的兩種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • Netty + ZooKeeper 實現(xiàn)簡單的服務注冊與發(fā)現(xiàn)

    Netty + ZooKeeper 實現(xiàn)簡單的服務注冊與發(fā)現(xiàn)

    服務注冊和發(fā)現(xiàn)一直是分布式的核心組件。本文介紹了借助 ZooKeeper 做注冊中心,如何實現(xiàn)一個簡單的服務注冊和發(fā)現(xiàn)。,需要的朋友可以參考下
    2019-06-06

最新評論