java - 使用 Selenium 截屏

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

最佳答案

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

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

关于java - 使用 Selenium 截屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41672961/

相关文章:

java - Jsch - 每个 session 多个 channel

java - Android O 固定快捷方式 - CREATE_SHORTCUT Intent 过滤器

ios - 从 Youtube YTPlayerView 获取当前帧

WPF 屏幕截图不包括标题栏

winforms - C#的winforms控件的屏幕截图

java - Android/Java 关于线程的决定

java - JLayer - 暂停和恢复歌曲

node.js - Nightwatchjs 标签在 Chrome 的 headless 模式下切换不起作用

google-chrome - 无法在 headless 模式下最大化 Chrome 窗口

java - 我如何防止 Selenium RC 在我的测试运行时窃取窗口焦点?