Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 97773

Need Help Using Selenium to take screenshot of every page within a pdf

$
0
0

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). I will appreciate anyone who can help me solve this problem.

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;    }}

Viewing all articles
Browse latest Browse all 97773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>