美文网首页
Selenium实现网页截图

Selenium实现网页截图

作者: 小熊哈哈 | 来源:发表于2020-04-06 14:07 被阅读0次

    近期在做基于WebGL的网页三维展示截图,PhantomJS支持浏览器截图,但由于PhantomJS是一个阉割版的webkit,不支持flash、webGL、video/audio等功能,只好采用Selenium的方案,实现如下:

    1.配置ChromeDriver

    下载chromeDriver(要与chrome版本匹配):

    http://chromedriver.storage.googleapis.com/index.html

    配置chromeDriver

    将chromeDriver.exe 放在chrome浏览器根目录下

    2.功能实现

    添加依赖

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        
    </dependencies>
    

    实现截图工具类

    package org.example.phantomTest;
    
    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;
    import java.util.Date;
    
    public class SeleniumTools {
        /**
         * 浏览器截图
         *
         * @param url          页面地址
         * @param storage_path 本地存储路径
         * @param file_name    存储文件名
         * @param millis       截屏延时 毫秒
         * @throws IOException 执行异常
         */
        public static void screenShot(String url, String storage_path, String file_name, long millis) throws IOException {
            System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
    
            //初始化 ChromeDriver
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments( "start-fullscreen", "allow-running-insecure-content");
            //chromeOptions.addArguments( "--headless", "allow-running-insecure-content");
            WebDriver driver = new ChromeDriver(chromeOptions);
    
            //访问 URL
            driver.get(url);
    
            //延时,等待WebGL渲染完成
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            //截图并进行存储
            File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            File destFile = new File(storage_path + File.separator + file_name);
            FileUtils.copyFile(src, destFile);
    
            //关闭 ChromeDriver
            driver.close();
            driver.quit();
        }
    
        public static void main(String[] args) {
            String url = "http://cesium.marsgis.cn/cesium-example/editor.html?data=qx-simiao#35_3dtiles";
            String storage_path = "D:\\selenium";
            long timeStamp = new Date().getTime();
            try {
                screenShot(url, storage_path, timeStamp + ".png", 20000);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("执行完成");
        }
        
    }
    

    注意:多任务处理时,需要考虑多线程问题

    Demo源码:

    链接:https://pan.baidu.com/s/1p-CqVlOkxY3zIt1ByuwwWw
    提取码:oyji

    附录:

    ChromeDriver配置项

    参考:https://sites.google.com/a/chromium.org/chromedriver/capabilities

    相关文章

      网友评论

          本文标题:Selenium实现网页截图

          本文链接:https://www.haomeiwen.com/subject/hixbphtx.html