Priyank Thakkar
Priyank Thakkar

Reputation: 4852

Selenium Screen Capture - Image Unavailable

I was trying capture the screen for http://www.flipkart.com url using selenium with firefox.

public class App {

    private static final String APP_URL = "http://www.flipkart.com";

    public static void main(String[] args) {
        WebDriver webDriver = null;
        try {
            webDriver = new FirefoxDriver();
            webDriver.get(APP_URL);
            webDriver.manage().window().maximize();

            if (webDriver instanceof TakesScreenshot) {
                TakesScreenshot screenshot = (TakesScreenshot) webDriver;
                File imageFile = screenshot.getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(imageFile, new File(
                        "C:\\Captures\\captured.png"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (webDriver != null) {
                webDriver.quit();
            }
        }
    }
}

It takes the screen shot of the full page, but the inside page it show Image Unavailable for many other images. I am not able to correct it. Help me.

Screen Shot Taken With Selenium

Upvotes: 2

Views: 506

Answers (3)

samson
samson

Reputation: 1607

Use this function to capture image for opened page, and placed with corresponding folder path.but all images are stored with the format of png.

 public static void captureScreenShot(WebDriver ldriver) {

        // Take screenshot and store as a file format
        File src = ((TakesScreenshot) ldriver).getScreenshotAs(OutputType.FILE);
        try {
            // now copy the screenshot to desired location using copyFile method
            // title=ldriver.getTitle();
            FileUtils.copyFile(src, new File(System.getProperty("user.dir") + "\\src\\data\\" + siteCapture + "\\"
                    + System.currentTimeMillis() + ".png"));
        }

        catch (IOException e)

        {

            System.out.println(e.getMessage());

        }

    }

Upvotes: 0

Vicky
Vicky

Reputation: 3021

The best solution would be to scroll through the page and then take the screenshot

  //scroll to the bottom of the page
 ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
 ////scroll to the top of the page
 ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0,0)");

Add these line before taking screenshot

I tried with the above solution it worked fine

Hope this helps you...Kindly get back if you have any queries

Upvotes: 6

peetya
peetya

Reputation: 3628

The reason is that the page is loading the images by Ajax. Put a Thread.sleep() before making the screenshot. It is not a nice solution, but it should work :)

Upvotes: 1

Related Questions