在 Selenium 中進行網頁截圖的最佳方法?
我們可以使用 Selenium 進行網頁截圖。這是一個三步的過程。進行截圖是缺陷和故障分析中至關重要的步驟之一。
首先,我們必須將驅動程式物件轉換為 TakeScreenshot 介面。
語法
TakesScreenshot s = (TakesScreenshot)driver;
接下來,我們必須藉助 getScreenshotAs 方法來獲取影像檔案,最後使用 FileUtils.copyFile 方法將檔案複製到某個位置。
語法
File src=s.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("file path"));示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;
import java.io.File;
public class ScreenshotCapture{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://tutorialspoint.tw/about/about_careers.htm");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// screenshot capturing
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("careerpage.png"));
driver.quit();
}
}
廣告
資料結構
計算機網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP