Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take full page Screenshot of a scrollable webpage using Selenium WebDriver with Java?

Existing code which takes screenshot of only visible screen. I am using the Chromedriver.

  System.setProperty("webdriver.chrome.driver", "D:/chromedriver/chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  driver.get("http://www.bbc.com");       
  driver.manage().window().maximize();
  System.out.println(driver.getTitle());
  File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(scrFile, new File("D:/chromedriver/scr3.png"));
  driver.close();
like image 216
Sarthak Srivastava Avatar asked Mar 23 '17 10:03

Sarthak Srivastava


People also ask

How do I take a screenshot of the whole scrolling page?

How do I take a scrolling screenshot on Android? On Android 11 or later, press the power and volume down buttons at the same time. Then, tab the “capture more” option at the bottom left corner of the screen.

Can Selenium capture screenshot scrolling window?

takeScreenshot(driver); Here 1000 is scrolled out time in milliseconds, so for taking a screenshot, the program will scroll for each 1000 msec. Step 2): Now, get the image from the screenshot and write it to the file. You can provide the file type as jpg, png, etc.

How do you take a screenshot of a page in Java?

We use java.It provides method like createScreenCapture which captures the current screen. This method returns captured image as BufferedImage object which can be saved as a file. It also uses ImageIO to save it as PNG image format.


3 Answers

Please Find the below code, you can scroll and take screenshots as many as you want. Note the elements Webelement. Store them and scroll relatively. You can Scroll depending upon how many screenshots you want.

driver.get("http://www.bbc.com");       
driver.manage().window().maximize();
System.out.println(driver.getTitle());
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:/chromedriver/scr3.png"));  
WebElement elements = driver.findElement(By.xpath(".//*[@id='page']/section[6]/div/div/div[1]/ul/li[3]/div/div[2]/h3/a"));    
Thread.sleep(3000L);
JavascriptExecutor js = (JavascriptExecutor) driver;
int yPosition = elements.getLocation().getY();
js.executeScript("window.scroll (0, " + yPosition + ") ");       
Thread.sleep(3000L);         
File scrFile1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile1, new File("D:/chromedriver/scr4.png"));
driver.close();
like image 113
Kishan Patel Avatar answered Oct 24 '22 01:10

Kishan Patel


You can't do this using merely selenium. You need other tool to perform your task. Follow the link and see my answer: Screen shot issue in selenium webdriver

Hope, it may help you.

like image 34
optimistic_creeper Avatar answered Oct 24 '22 00:10

optimistic_creeper


To take screenshot of a complete webpage, we have to use a third party utility called 'aShot'. aShot is a WebDriver Schreenshot Utility with which we can take screenshot of the entire webpage & also individual WebElement. To do this, we have to download the aShot jar file & add to our project along with Selenium jar files.

like image 21
Usha Avatar answered Oct 23 '22 23:10

Usha