Click here to Skip to main content
15,882,954 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
package tenis2;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class screenshot {
public static void main(String[] args)throws IOException{

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com/");
driver.manage().window().maximize();
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\new\\screenshot1.png"), true);

driver.quit();
}
}

What I have tried:

The code above actually contains the scripts to take a screenshot. I actually took this code from one of the sites. But when the same code is being tried by me it throws an error in the following two lines saying "add cast to file". Can anyone help me out with this

File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\new\\screenshot1.png"), true);

These are the two line
Posted
Updated 13-Apr-17 4:57am
Comments
Richard MacCutchan 13-Apr-17 10:48am    
What is the exact text of the error message?

1 solution

The compiler is telling you that the object returned from the getScreenshotAs method is not a File, which is what you're trying to assign it to.

You might be tempted to force the assignment using a cast like this:
Java
File scrFile = (File) ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\new\\screenshot1.png"), true);

However that's not the right solution. Look right at the top of your code (well done for posting all of it!) where we can see that you have this import:
Java
import org.apache.commons.io.FileUtils;

Whereas you need:
Java
import java.io.File;

I think that if you change this import it should compile without complaint and without need for that cast mentioned above.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900