Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
314 views
by The go-to Tester (218 points)

I am running Selenium webdriver tests and want to take screenshot of the page after loading. But unfortunately I am not getting screenshot of whole page. Screen shot is taken before the whole page loads. 

My code is as follows-:

 System.setProperty("webdriver.gecko.driver", "C://Selenium driver/geckodriver.exe");

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver();
driver.get("http://www.snapdeal.com");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("E:\\screenshot.png"));
driver.close();
 
Screen shot is taken befor the complete page loads as shown below-:

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer

I checked snapdeal website and your page is loaded with infinity scroll. So everytime you scroll down, new iteams will be added to the page dynamically.

In this case, you can identify any item from the footer area and explicitly wait till the item is loaded. This way, WebDriver will wait till the footer is loaded and move to the next command.

Eg.

System.setProperty("webdriver.gecko.driver", "C://Selenium driver/geckodriver.exe");

DesiredCapabilities capabilities = DesiredCapabilities.firefox();

capabilities.setCapability("marionette", true);

WebDriver driver = new FirefoxDriver();

driver.get("http://www.snapdeal.com");

//WebDriverWait wait = new WebDriverWait(driver,10);

//wait.until(ExpectedConditions.elementIsVisible("Your element identify by here"));

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File("E:\\screenshot.png"));

driver.close();
 
Hope that helps!
 


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...