如何在 Selenium 中捕獲螢幕截圖?
我們可以藉助 TakesScreenshot 介面在 Selenium 中捕獲螢幕截圖。getScreenshotAs() 方法用於捕獲螢幕截圖並將其儲存在特定位置。
示例
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
public class ScreenshotFull{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://tutorialspoint.tw/questions/index.php";
driver.get(url);
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// capture screenshot with getScreenshotAs() and store in the //Screenshots folder
TakesScreenshot s = (TakesScreenshot)driver;
File source = s.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File("./Screenshots/tutorialpoint.png"));
driver.quit();
}
}
廣告
資料結構
聯網
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP