Unable to take screenshot

The below statement worked really fine with Selenium webdriver 2.39. After upgrading this to webdriver 2.40, below statement does not work.

            File scrFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir")+"/output//files/"+fileName));

It throws this exception. org.openqa.selenium.chrome.ChromeDriver cannot be cast to org.openqa.selenium.TakesScreenshot

anyone has anyidea on how to fix this?

EDIT: I did not modify the above code. Only change i did is to replace the selenium-java-2.39.0.jar with the selenium-java-2.40.0.jar.


It might be worth checking that you're using the Java 7 compiler. Here's my snippet working fine with Chrome:

TakesScreenshot yourScreenshot = ( TakesScreenshot ) driver;

File tempfile = yourScreenshot.getScreenshotAs( OutputType.FILE );
System.out.println( tempfile.getAbsolutePath() );

File myScreenshotDirectory = new File( "Screenshots Folder" );
myScreenshotDirectory.mkdirs();

File screenshotImage = new File( myScreenshotDirectory,"ScreenShotImage.png" );
FileUtils.moveFile( tempfile, screenshotImage );

The issue is resolved.

I had selendroid jar as well associated to the test(which is used to run my tests in Android devices). Due to the order of the jars in the classpath, selendroid jar was causing the issue. When i changed the order (selenium jar first , selendroid next), error is gone.

Thank You All.

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

上一篇: 仅在after方法中使用selenium(java)截图

下一篇: 无法截图