我正在结合使用 Selenium 和 Java 来学习自动化。对于这个特定问题,我尝试使用 Selenium 转到包含 PDF 的 Google Drive 链接,然后截取 PDF 的每一页的屏幕截图,然后将每个屏幕截图保存在用户可以自己命名的文件中。问题是它截取第一页的屏幕截图,而不是向下滚动到 PDF 的每一页。这是我正在使用的测试链接(https://drive.google.com/file/d/1optp32jv20rvyiSBcCdI_a7C1jRR1UT4/view)。
下面是我正在使用的代码:
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;
}
}