Selenium Webdriver實現(xiàn)截圖功能的示例
前幾天在研究中自動化的時候突發(fā)奇想,想著能不能來截個圖,以便之后查看,實現(xiàn)的方法其實也不難,畢竟selenium webdriver已經(jīng)提供了截圖額功能,TakesScreenshot接口函數(shù)(英文意思就是獲取屏幕截圖takes-screenshot)。
廢話不多說了,直接上代碼
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實現(xiàn)截圖功能的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
IDEA Maven源修改為國內(nèi)阿里云鏡像的正確方式
為了加快 Maven 依賴的下載速度,可以將 Maven 的中央倉庫源修改為國內(nèi)的鏡像,比如阿里云鏡像,以下是如何在 IntelliJ IDEA 中將 Maven 源修改為阿里云鏡像的詳細步驟,感興趣的同學可以參考閱讀一下2024-09-09
Spring Data Jpa+SpringMVC+Jquery.pagination.js實現(xiàn)分頁示例
本文介紹了Spring Data Jpa+SpringMVC+Jquery.pagination.js實現(xiàn)分頁示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Springboot整合SpringSecurity實現(xiàn)登錄認證和鑒權全過程
這篇文章主要介紹了Springboot整合SpringSecurity實現(xiàn)登錄認證和鑒權全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Javaweb動態(tài)開發(fā)最重要的Servlet詳解
動態(tài)web的核心是Servlet,由tomcat解析并執(zhí)行,本質(zhì)是Java中的一個類(面向?qū)ο螅┻@個類的功能十分強大幾乎可以完成全部功能,在Java規(guī)范中只有Servlet實現(xiàn)類實例化的對象才能被瀏覽器訪問,所以掌握Servlet具有重要意義2022-08-08
解決SpringBoot加載application.properties配置文件的坑
這篇文章主要介紹了SpringBoot加載application.properties配置文件的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

