前幾天在研究中自動化的時候突發奇想,想著能不能來截個圖,以便之后查看,實現的方法其實也不難,畢竟selenium webdriver已經提供了截圖額功能,TakesScreenshot接口函數(英文意思就是獲取屏幕截圖takes-screenshot)。
廢話不多說了,直接上代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.wch; import java.io.File; import java.io.IOException; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.WebDriverWait; import com.sun.jna.platform.FileUtils; public class TestTakesScreenshot { public static void main(String[] args) { System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com"); File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //講截取的圖片以文件的形式返回 try { org.apache.commons.io.FileUtils.copyFile(srcFile, new File("d:\\screenshot.png")); //使用copyFile()方法保存獲取到的截圖文件 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } driver.quit(); } } |
還有其他方法的話希望各位也能提供下,互相學習。
以上這篇Selenium Webdriver實現截圖功能的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/woiangaoiowe/article/details/60963291