使用chromedriver抓取网页截图

前提:

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方式比较,这样截出来的图片会更加清晰)

 

posted @ 2024-11-07 16:37  信铁寒胜  阅读(103)  评论(0编辑  收藏  举报