当我尝试使用以下代码进行屏幕截图时,显示错误。
FileUtils.copyFile(source, new File("*./Screenshots/facebook.png"));
但是当我尝试以下代码时,没关系。
FileHandler.copy(source,new File("*./Screenshots/facebook.png"));
为什么?
完整代码位于
之下package sample.code;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
import org.testng.annotations.Test;
public class ScreenShot {
@Test
public void facebookScreenShot() throws IOException {
WebDriver driver= new ChromeDriver();
driver.get("http://www.facebook.com");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[@id='u_0_m']")).sendKeys("screenshot");
TakesScreenshot ts=(TakesScreenshot)driver;
File source=ts.getScreenshotAs(OutputType.FILE);
FileHandler.copy(source,new File("*./Screenshots/facebook.png"));
driver.quit();
}
}
答案 0 :(得分:1)
通过使用Robot类,您可以截取屏幕截图。以下是截屏的代码。
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class CaptureScreenshot {
public static void main(String[]args) throws IOException, HeadlessException, AWTException
{
// This code will capture screenshot of current screen
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
// This will store screenshot on Specific location
ImageIO.write(image, "png", new File("C:\\Users\\Screenshot\\CurrentScreenshot.png"));
}
}