Selenium Webdriver實現截圖功能的示例
前幾天在研究中自動化的時候突發(fā)奇想,想著能不能來截個圖,以便之后查看,實現的方法其實也不難,畢竟selenium webdriver已經提供了截圖額功能,TakesScreenshot接口函數(英文意思就是獲取屏幕截圖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實現截圖功能的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Data Jpa+SpringMVC+Jquery.pagination.js實現分頁示例
本文介紹了Spring Data Jpa+SpringMVC+Jquery.pagination.js實現分頁示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Springboot整合SpringSecurity實現登錄認證和鑒權全過程
這篇文章主要介紹了Springboot整合SpringSecurity實現登錄認證和鑒權全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Javaweb動態(tài)開發(fā)最重要的Servlet詳解
動態(tài)web的核心是Servlet,由tomcat解析并執(zhí)行,本質是Java中的一個類(面向對象)這個類的功能十分強大幾乎可以完成全部功能,在Java規(guī)范中只有Servlet實現類實例化的對象才能被瀏覽器訪問,所以掌握Servlet具有重要意義2022-08-08解決SpringBoot加載application.properties配置文件的坑
這篇文章主要介紹了SpringBoot加載application.properties配置文件的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08