使用chromedriver抓取網頁截圖

信铁寒胜發表於2024-11-07

前提:

1、電腦安裝了谷歌瀏覽器

2、下載chromedriver-win64,放到C:\Program Files\Google\Chrome\chromedriver-win64 安裝路徑

chromedriver-win64 下載地址:Chrome for Testing availability

3、

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws InterruptedException, IOException {
        WebDriver driver = null;
        try{
            System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\chromedriver-win64\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--headless"); // 無介面模式
            options.addArguments("--hide-scrollbars"); // 隱藏捲軸(可選)
            options.addArguments("--force-device-scale-factor=2"); // 設定縮放因子為 2,提高畫質晰度

            driver = new ChromeDriver(options);
            driver.get("https://www.baidu.com/"); // 替換為你想截圖的網頁
            Thread.sleep(20000);
            File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            File imageFile = new File("D:\\筆記\\test.png");
            FileUtils.copyFile(screenshot, imageFile); // 儲存截圖
        }finally {
            if(driver != null){
                driver.quit();
            }
        }
    }
}

  效果圖(會看到看到明顯截圖是不全的。)是因為設定了縮放因子,提高畫質晰度導致的

如何處理這種情況呢。

A、新增一個模擬滾動調整

 /**
     * 模擬捲軸
     * @param driver
     * @throws InterruptedException
     */
    public static void analogScrollBar(WebDriver driver) throws InterruptedException {
        // 模擬捲軸,解決懶載入問題
        String jsHeight = "return document.body.clientHeight";
        long heightIndex = (long) ((JavascriptExecutor) driver).executeScript(jsHeight);
        long k = 1;
        // 模擬手動捲軸
        while (k * 500 < heightIndex) {
            String jsMove = "window.scrollTo(0, " + (k * 500) + ")";
            System.out.println(jsMove);
            ((JavascriptExecutor) driver).executeScript(jsMove);
            Thread.sleep(200);
            heightIndex = (long) ((JavascriptExecutor) driver).executeScript(jsHeight);
            k++;
        }
        // 獲取網頁的寬度和高度
        String jsWidth = "return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);";
        String jsHeightFull = "return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);";
        long widthIndex = (long) ((JavascriptExecutor) driver).executeScript(jsWidth);
        heightIndex = (long) ((JavascriptExecutor) driver).executeScript(jsHeightFull);

        System.out.println("Width: " + widthIndex + ", Height: " + heightIndex);

        // 設定瀏覽器視窗大小
        driver.manage().window().setSize(new Dimension((int) widthIndex + 100, (int) heightIndex + 500));
    }

效果圖:

 B、去掉縮放因子(A方式比較,這樣截出來的圖片會更加清晰)

相關文章