
我們可以使用Selenium模擬列印螢幕按鈕。截圖是
captured with the Print screen button. Capturing a screenshot is a three way
process. It is an important step towards failure analysis.
We shall convert the driver object to TakeScreenshot# interface.
#Syntax
#
處理。這是朝著故障分析的重要一步。
我們將把驅動程式物件轉換為TakeScreenshot介面。
語法
1 | TakesScreenshot s = (TakesScreenshot)driver;
|
登入後複製
然後,使用getScreenshotAs#方法,我們將得到一個映像文件,並使用FileUtils.copyFile方法將該文件複製到指定位置。
語法
1 2 | File sp=s.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sp, new File( "path of image file" ));
|
登入後複製
Example
的中文翻譯為:
範例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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://www.tutorialspoint.com/index.htm" );
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File( "logopage.png" ));
driver.quit();
}
}
|
登入後複製
以上是如何使用Java中的Selenium WebDriver模擬按下Print Screen按鈕?的詳細內容。更多資訊請關注PHP中文網其他相關文章!