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

  • What is "initial stage page of Webdriver server"? – Alexey R. Mar 28 '18 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 Mar 28 '18 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. Mar 28 '18 at 13:32
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.

  • 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. Mar 28 '18 at 15:32
  • the get method does not a wait automatically – Amias Mar 28 '18 at 18:27
  • What you're saying contradicts to this post stackoverflow.com/q/5664808/8343843 – Alexey R. Mar 28 '18 at 19:26
  • Agree, give the script some time before the page shows up. Wait for some element, may be google logo... – Tired Of Testing Mar 28 '18 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 Mar 29 '18 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.

  • Its failing with Explicit wait as well.. as it doesn't find element – Ketan Mar 29 '18 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 Mar 30 '18 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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