使用Selenium 3.6截图

我无法在我的项目中截取屏幕截图。 我正在使用Selenium 3.6版本的Java。 这是我的代码:

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:tmpscreenshot.png"));

不幸的是,“copyFile”给出了以下错误:

The method copyFile(File, File) is undefined for the type FileUtils

我还导入了所有必需的软件包。

任何人都可以帮我截图吗?


导入使用import org.apache.commons.io.FileUtils 。 这导入您需要的FileUtils类。

猜猜你输入了错误的软件包

检查这些包应该在那里

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

示例代码:

public class Takenscreensshot {
    public static void main(String[] args) throws IOException {
        WebDriver driver=new FirefoxDriver();
        driver.get("https://www.google.co.in");
        File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(src, new File("d:/ss.png"));
        driver.close();
    }
}

错误表示它所有the "copyFile" is giving an error. It is saying "The method copyFile(File, File) is undefined for the type FileUtils" the "copyFile" is giving an error. It is saying "The method copyFile(File, File) is undefined for the type FileUtils" 。 可能FileUtils在导入中有多个定义。 所以解决方案如下:

  • 仅使用:

    import org.apache.commons.io.FileUtils;
    
  • 要么

  • 将您的代码更改为:

    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
    org.apache.commons.io.FileUtils.copyFile(scrFile, new File("C:tmpscreenshot.png"));
    

  • 尝试

    String capture = "window.png";
    
        try {
    
            Thread.sleep(3000);
    
            byte screenshot[] = (byte[])((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
    
        FileOutputStream fos = new FileOutputStream(capture);
                    fos.write(screenshot);
    
            }catch (Exception){ }
    

    这对我来说正确。

    链接地址: http://www.djcxy.com/p/67401.html

    上一篇: Taking Screenshot using Selenium 3.6

    下一篇: Selenium : Unable to take a screenshot of failed Login Page