1

Using this code trying to take screenshot, but Selenium web driver takes screenshot of initial stage page of Webdriver server and not google page

System.setProperty("webdriver.ie.driver", ......"); driver = new InternetExplorerDriver(); driver.manage().window().maximize(); driver.get("http://www.google.com");

    File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    try {
    FileUtils.copyFile(src, new File("PATH"));
    System.out.println("Screenshot taken");
    }

    catch (IOException e)
     {
      System.out.println(e.getMessage());

     }

Could you please help to resolve this issue. Tried with adding thread.sleep also ut not working. Need screenshot of google home page using above code

3
  • What is "initial stage page of Webdriver server"?
    – Alexey R.
    Commented Mar 28, 2018 at 13:21
  • If you check the screenshot.. that message first get displayed on browser when browser opens and then google page open. System is capturing that screenshot and not google homepage screenshot
    – Ketan
    Commented Mar 28, 2018 at 13:23
  • Try to run the code in debug mode. Set the breakpoint on 'File src' line wait for a while and proceed the execution. Clean up 'P:\\Selenium\\Selenium Output\\' folder previously.
    – Alexey R.
    Commented Mar 28, 2018 at 13:32

2 Answers 2

3

I'm not sure how long you waited, but with your current code the screenshot is taken a split millisecond after starting the navigation.

You should wait for an element to be visible (e.g. the Google Search button) before taking a screenshot.

5
  • Waiting for an element to be visible is a good advice. However doesn't "get" method wait for page gets loaded? "get" might introduce some error into onload event firing but I am not sure the error for google home would be so large. I believe in this particular case there is the issue of different nature.
    – Alexey R.
    Commented Mar 28, 2018 at 15:32
  • the get method does not a wait automatically
    – Amias
    Commented Mar 28, 2018 at 18:27
  • What you're saying contradicts to this post stackoverflow.com/q/5664808/8343843
    – Alexey R.
    Commented Mar 28, 2018 at 19:26
  • Agree, give the script some time before the page shows up. Wait for some element, may be google logo... Commented Mar 28, 2018 at 21:01
  • Alexey: depends on how the page loads , if it's very ajax it can be non obvious when it's stopped loading
    – Amias
    Commented Mar 29, 2018 at 6:34
2

You haven't given the page enough time to load so you need to wait for it to load.

waiting can be tricky in selenium because its event based so you need to decide weather to use and implicit or explicit waits, this setting will effect the rest of your selenium calls so choose carefully. Using both will make waits unpredictably long in baffling ways.

This is fully discussed with examples in the selenium docs here. https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

implicit waits are better if you think there is a chance of your selenium driver going away (e.g. using a grid) and explicit waits can be more precise but will take the full timeout if the driver crashes.

2
  • Its failing with Explicit wait as well.. as it doesn't find element
    – Ketan
    Commented Mar 29, 2018 at 6:56
  • You might need to share more info on the return value of the get and what your logs say about the get.
    – Amias
    Commented Mar 30, 2018 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.