javaselenium-webdriver

How to use Selenium to take screenshot of every page within a PDF


I am using Selenium and Java together to learn automation. For this specific problem, I am trying to use Selenium to go to a Google Drive link that contains a PDF and then take a screenshot of every single page of the PDF and then save each screenshot in a file the user can name themselves. The problem is that it is taking screenshots of the first page and not scrolling down to each individual page of the PDF. Here is the test link that I am working with(https://drive.google.com/file/d/1optp32jv20rvyiSBcCdI_a7C1jRR1UT4/view).

Below is the code that I am using:

package googleDriveScreenshotTest;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Scanner;

public class PdfScreenShotTest2{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Get user inputs with validation
        String pdfUrl = getUserInput(scanner, "Enter Google Drive PDF link: ");
        String folderName = getUserInput(scanner, "Enter folder name to save images: ");
        int totalPages = getValidPageCount(scanner);
        
        // Setup WebDriver
        WebDriver driver = new ChromeDriver();
        
        try {
            // Create directory with error handling
            File screenshotDir = createScreenshotDirectory(folderName);
            
            // Navigate to PDF
            driver.get(pdfUrl);
            driver.manage().window().maximize();
            
            // Wait for document to load
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
            WebElement body = wait.until(ExpectedConditions.elementToBeClickable(By.tagName("body")));
            
            // Click anywhere on page
            body.click();
            
            // Take screenshots of each page
            for (int pageNumber = 1; pageNumber <= totalPages; pageNumber++) {
                File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                File destination = new File(screenshotDir + "/page_" + pageNumber + ".png");
                
                try {
                    FileHandler.copy(screenshot, destination);
                    System.out.println("Saved: " + destination.getAbsolutePath());
                    
                    if (pageNumber < totalPages) {
                        body.sendKeys(Keys.PAGE_DOWN);
                        Thread.sleep(2000); // Allow time for scrolling
                    }
                } catch (IOException e) {
                    System.err.println("Error saving screenshot for page " + pageNumber + ": " + e.getMessage());
                }
            }
            
        } catch (Exception e) {
            System.err.println("An error occurred: " + e.getMessage());
        } finally {
            driver.quit();
            scanner.close();
        }
    }

    private static String getUserInput(Scanner scanner, String prompt) {
        while (true) {
            System.out.print(prompt);
            String input = scanner.nextLine().trim();
            if (!input.isEmpty()) {
                return input;
            }
            System.out.println("Input cannot be empty. Please try again.");
        }
    }

    private static int getValidPageCount(Scanner scanner) {
        while (true) {
            try {
                System.out.print("Enter number of pages in PDF: ");
                int count = scanner.nextInt();
                scanner.nextLine(); // Consume newline
                if (count > 0) {
                    return count;
                }
                System.out.println("Please enter a positive number.");
            } catch (Exception e) {
                System.err.println("Invalid input. Please enter a valid number.");
                scanner.nextLine(); // Clear invalid input
            }
        }
    }

    private static File createScreenshotDirectory(String folderName) throws IOException {
        String downloadPath = System.getProperty("user.home") + "/Downloads/" + folderName;
        File folder = new File(downloadPath);
        
        if (!folder.exists()) {
            if (!folder.mkdirs()) {
                throw new IOException("Failed to create directory: " + downloadPath);
            }
        }
        
        return folder;
    }
}

Solution

  • This wasn't as easy as I thought it would be. I found that none of the JS scroll methods work on this page. Rather than taking a screenshot of the page, I took screenshots of each IMG tag that corresponds to a slide. This also scrolled the page, so...

    The code below works... it takes a screenshot of each slide. It first parses the control that displays the number of pages in the PDF and then loops through each IMG tag and takes a screenshot of it. You'll likely want to change the output path.

    import java.io.File;
    import java.time.Duration;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.commons.io.FileUtils;
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class PdfScreenShotTest2 {
        public static void main(String[] args) throws Exception {
            String url = "https://drive.google.com/file/d/1optp32jv20rvyiSBcCdI_a7C1jRR1UT4/view";
    
            WebDriver driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get(url);
    
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
            JavascriptExecutor js = (JavascriptExecutor) driver;
    
            // get last page number
            WebElement label = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[aria-label^='Page 1 of']")));
            String fullLabel = label.getAttribute("aria-label");
            int lastPage = Integer.parseInt(fullLabel.substring(fullLabel.lastIndexOf(' ') + 1));
    
            // get screenshots of each slide
            List<WebElement> slides = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div[role='document'] img")));
            for (int i = 0; i < lastPage; i++) {
                File slideFile = slides.get(i).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(slideFile, new File("c:\\temp\\slide" + i + ".png"));
                slides = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div[role='document'] img")));
            }
    
            driver.quit();
        }
    }