如何在 Java 中使用Selenium WebDriver模擬 Print screen 按鈕?
我們可以使用 Selenium 來模擬 Print screen 按鈕。螢幕截圖就是使用 Print screen 按鈕捕獲的。捕獲螢幕截圖需要三個步驟。這是進行故障分析的重要步驟。
我們應將驅動程式物件轉換為TakeScreenshot介面。
語法
TakesScreenshot s = (TakesScreenshot)driver;
然後,使用getScreenshotAs方法生成一個圖片檔案,並使用FileUtils.copyFile方法將檔案複製到某個位置。
語法
File sp=s.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sp, new File("path of image file"));示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;
import java.io.File;
public class PrintScreenSimulate {
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/index.htm");
// screenshot capturing
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("logopage.png"));
driver.quit();
}
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言教程
C++
C#
MongoDB
MySQL
Javascript
PHP