使用Selenium拍摄屏幕截图

时间:2017-01-16 09:20:54

标签: java selenium screenshot

我从另一个包中导入了这个类,并尝试调用此方法,但它无效。

当我在同一个类中创建此方法并调用它时,它正在工作。

 private void getScreenshot() throws IOException
 {
      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY/hh-mm-ssaa");
      String destfile = dateFormat.format(new Date()) + ".png";
      FileUtils.copyFile(scrFile, new File("D:\\workspace\\Firewall\\Resources\\ScreenShots\\"+destfile));
 }

2 个答案:

答案 0 :(得分:1)

我猜主要原因是你导入了错误的库。退房:

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

如果您的库是相同的,请尝试使用我的方法:

public class TakeScreenshot {
    WebDriver driver;
    public TakeScreenshot(WebDriver driver){
        this.driver = driver;
    }
 public void ScreenShot(String nameTc)
{
// Take screenshot and store as a file format
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
 // now copy the  screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("bin/" + nameTc + ".png"));
}
catch (IOException e)
 {
  System.out.println(e.getMessage());
 }} }

答案 1 :(得分:0)

通过使用此功能,您可以捕获屏幕截图,只需调用captureScreenShot()方法通过发送文件路径来截取屏幕截图

...
File ".../python-sandbox/googleapiclient/http.py", line 842, in execute
    raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 401 when requesting https://www.googleapis.com/youtube/v3/channels?alt=json&part=snippet%2CcontentDetails%2Cstatistics&forUsername=GoogleDevelopers returned "Invalid Credentials">