【问题标题】:Taking a screen shot using Selenium使用 Selenium 截屏
【发布时间】:2017-01-16 09:20:54
【问题描述】:

我从另一个包中导入了这个类并尝试调用这个方法,但它不起作用。

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

 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));
 }

【问题讨论】:

  • 尝试为您的问题添加一些格式,因为阅读您的代码非常困难。如果有任何异常日志也要添加
  • 如果你在另一个类中有这个确切的方法,它不起作用,因为它是private。如果是这种情况,您应该将其更改为public

标签: java selenium screenshot


【解决方案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());
 }} }

【讨论】:

    【解决方案2】:

    使用这个可以截屏,只需要通过发送文件路径调用captureScreenShot()方法截屏

      public static void captureScreenShot(String filePath) 
      { 
      File scrFile =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
       try 
      {   
           FileUtils.copyFile(scrFile, new File(filePath)); } 
        catch (IOException e)
       {
       // TODO Auto-generated catch block 
        e.printStackTrace(); }} 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      相关资源
      最近更新 更多