怎么在Java中利用selenium實現(xiàn)截圖操作

怎么在Java中利用selenium實現(xiàn)截圖操作?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

專業(yè)從事做網(wǎng)站、網(wǎng)站設計,高端網(wǎng)站制作設計,小程序定制開發(fā),網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團隊竭力真誠服務,采用H5高端網(wǎng)站建設+CSS3前端渲染技術(shù),響應式網(wǎng)站建設,讓網(wǎng)站在手機、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項小組,與您實時在線互動,隨時提供解決方案,暢聊想法和感受。

Java的特點有哪些

Java的特點有哪些 1.Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,實現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進行復雜的編程。 2.Java具有簡單性、面向?qū)ο蟆⒎植际?、安全性、平臺獨立與可移植性、動態(tài)性等特點。 3.使用Java可以編寫桌面應用程序、Web應用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應用程序等。

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

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

怎么在Java中利用selenium實現(xiàn)截圖操作;

當然也是可以設置保存到其他目錄下: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)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

分享標題:怎么在Java中利用selenium實現(xiàn)截圖操作
當前路徑:http://www.muchs.cn/article16/gcepdg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供響應式網(wǎng)站、手機網(wǎng)站建設企業(yè)網(wǎng)站制作、網(wǎng)站設計ChatGPT、網(wǎng)站設計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名