Taking Screenshot using Selenium 3.6

I'm not able to take a screenshot in my project. I'm using Selenium 3.6 version with Java. Here is my code:

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

Unfortunately the "copyFile" gives the following error:

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

I have also imported all the required packages.

Can anyone help me to take screenshot?


Import Use import org.apache.commons.io.FileUtils . This imports the FileUtils class you need.

Guess you import wrong package

check these package should be there

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

Sample code:

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 error says it all 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" . It may be possible FileUtils is having multiple definition within your imports. So the solution would be as follows :

  • Use only:

    import org.apache.commons.io.FileUtils;
    
  • OR

  • Change your code as :

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

  • Try

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

    This works for me correct .

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

    上一篇: LocationManager常常调用onLocationChanged?

    下一篇: 使用Selenium 3.6截图