最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

FileUtils.copyFile(srcFile, new File("屏幕截圖", time + ".png"));“屏幕截圖”是我們自己創(chuàng)建的文件夾用來存放截圖文件,此文件夾在project(工程)的更目錄

;

當(dāng)然也是可以設(shè)置保存到其他目錄下: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();
		
		/**
		 * 截屏操作
		 * 圖片已當(dāng)前時(shí)間命名
		 */
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); //轉(zhuǎn)換時(shí)間格式
		String time = dateFormat.format(Calendar.getInstance().getTime()); //獲取當(dāng)前時(shí)間
		File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //執(zhí)行屏幕截取
		FileUtils.copyFile(srcFile, new File("屏幕截圖", time + ".png")); //利用FileUtils工具類的copyFile()方法保存getScreenshotAs()返回的文件;"屏幕截圖"即時(shí)保存截圖的文件夾
		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實(shí)現(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"));
	}

方法三:在測(cè)試的過程中,有時(shí)候不需要截取整個(gè)屏幕,只需要截取某個(gè)元素(或者目標(biāo)區(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()獲取時(shí)間戳的方法
		FileUtils.copyFile(elementSnapshot(element), new File("屏幕截圖", System.currentTimeMillis()+".png"));
		Thread.sleep(2000);
		driver.quit();
		
	}
 
	/**
	 * 部分截圖(元素截圖)
	 * 有時(shí)候需要元素的截圖,不需要整個(gè)截圖
	 * @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)建一個(gè)矩形使用上面的高度,和寬度
		Rectangle rect = new Rectangle(width, height);
		//元素坐標(biāo)
		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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于JavaScript動(dòng)態(tài)規(guī)劃編寫一個(gè)益智小游戲

    基于JavaScript動(dòng)態(tài)規(guī)劃編寫一個(gè)益智小游戲

    最近在學(xué)習(xí)動(dòng)態(tài)規(guī)劃相關(guān)的知識(shí),所以本文將利用動(dòng)態(tài)規(guī)劃編寫一個(gè)簡單的益智小游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-06-06
  • Java之單例模式實(shí)現(xiàn)方案詳解

    Java之單例模式實(shí)現(xiàn)方案詳解

    這篇文章主要介紹了Java之單例模式實(shí)現(xiàn)方案詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 一次 Java 內(nèi)存泄漏的排查解決過程詳解

    一次 Java 內(nèi)存泄漏的排查解決過程詳解

    這篇文章主要介紹了一次 Java 內(nèi)存泄漏的排查過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 詳解mybatis中的if-else的嵌套使用

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

    本文主要介紹了mybatis中的if-else的嵌套使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • SpringBoot實(shí)用小技巧之如何動(dòng)態(tài)設(shè)置日志級(jí)別

    SpringBoot實(shí)用小技巧之如何動(dòng)態(tài)設(shè)置日志級(jí)別

    這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)用小技巧之如何動(dòng)態(tài)設(shè)置日志級(jí)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用SpringBoot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Spring MVC整合FreeMarker的示例

    Spring MVC整合FreeMarker的示例

    這篇文章主要介紹了Spring MVC整合FreeMarker的示例,幫助大家更好的理解和使用Spring MVC,感興趣的朋友可以了解下
    2020-12-12
  • Spring七大事務(wù)傳遞機(jī)制深入分析實(shí)現(xiàn)原理

    Spring七大事務(wù)傳遞機(jī)制深入分析實(shí)現(xiàn)原理

    實(shí)際項(xiàng)目開發(fā)中,如果涉及到多張表操作時(shí),為了保證業(yè)務(wù)數(shù)據(jù)的一致性,大家一般都會(huì)采用事務(wù)機(jī)制,好多小伙伴可能只是簡單了解一下,遇到事務(wù)失效的情況,便會(huì)無從下手,下面這篇文章主要給大家介紹了關(guān)于Spring事務(wù)傳遞機(jī)制的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • Spring事件監(jiān)聽基本原理與使用詳解

    Spring事件監(jiān)聽基本原理與使用詳解

    這篇文章主要介紹了Spring事件監(jiān)聽基本原理與使用詳解,Spring的事件監(jiān)聽機(jī)制和發(fā)布訂閱機(jī)制是很相似的:發(fā)布了一個(gè)事件后,監(jiān)聽該類型事件的所有監(jiān)聽器會(huì)觸發(fā)相應(yīng)的處理邏輯,需要的朋友可以參考下
    2024-01-01
  • ActiveMQ結(jié)合Spring收發(fā)消息的示例代碼

    ActiveMQ結(jié)合Spring收發(fā)消息的示例代碼

    這篇文章主要介紹了ActiveMQ結(jié)合Spring收發(fā)消息的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-10-10
  • 關(guān)于Spring中的三級(jí)緩存解析

    關(guān)于Spring中的三級(jí)緩存解析

    這篇文章主要介紹了關(guān)于Spring中的三級(jí)緩存,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論

尼勒克县| 崇文区| 肇源县| 北票市| 茂名市| 禹州市| 伊宁县| 定州市| 绥中县| 长春市| 安吉县| 义乌市| 齐齐哈尔市| 嘉禾县| 临沭县| 黄梅县| 衡阳县| 尉氏县| 巴东县| 临安市| 乌审旗| 禄劝| 靖边县| 安西县| 泸定县| 衡阳市| 杨浦区| 深泽县| 札达县| 南川市| 马山县| 栾城县| 华容县| 台中县| 鲁甸县| 日照市| 鞍山市| 韶关市| 临江市| 噶尔县| 陈巴尔虎旗|