zoukankan      html  css  js  c++  java
  • 搭建 springboot selenium 网页文件转图片环境

    1. 环境准备

    需要有 chrome 浏览器 + chrome driver + selenium 客户端

    离线 chrome 下载地址

    # 64位 linux 系统
    https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
    
    # 64位 weindow 系统
    http://www.google.cn/chrome/browser/desktop/index.html?standalone=1&platform=win64
    
    # 32位 weindow 系统
    http://www.google.cn/chrome/browser/desktop/index.html?standalone=1&platform=win
    
    # 官网 chromedriver
    http://chromedriver.storage.googleapis.com/index.html
    
    # 淘宝 chromedriver 镜像
    https://npm.taobao.org/mirrors/chromedriver/

    说明:chrome 和  chromedriver 版本需要一致

    1.2 查看 chrome 版本信息。在chrome浏览器输入以下地址

    chrome://version/

    1.3 例如我选择的是

    2. 将 chromedriver 放到 chrome 安装目录下

     3. 在 springboot 项目中引入 selenium 依赖

    <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.141.59</version>
            </dependency>

    4 网页整页转图片代码实现

    public static void convertHtml2Image(String url) throws InterruptedException {
         // 设置 chromedriver 地址 System.setProperty(
    "webdriver.chrome.driver", "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe");
        // 关闭日志
         System.setProperty("webdriver.chrome.silentOutput", "true");
    //实例化一个Chrome浏览器的实例 ChromeOptions options = new ChromeOptions(); // 字符编码 utf-8 支持中文字符 options.addArguments("lang=zh_CN.UTF-8"); // 开启最大化 options.addArguments("–start-maximized"); options.addArguments("–no-sandbox"); // 开启无头模式 options.addArguments("--headless"); options.addArguments("--disable-gpu");
    // 关闭日志
    options.addArguments("--disable-logging"); WebDriver driver
    = new ChromeDriver(options); driver.get(url); Thread.sleep(3000); int height = Integer.parseInt((String) ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight.toString()")); int width = Integer.parseInt((String) ((JavascriptExecutor) driver).executeScript("return document.body.scrollWidth.toString()")); driver.manage().window().setSize(new Dimension(width, height)); driver.manage().window().maximize(); //截屏操作 //截图到output File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); try { String desImage = UUID.randomUUID().toString() + ".png"; //复制内容到指定文件中 FileUtil.copyFile(scrFile, new File(desImage)); System.out.println(desImage); } catch (IOException e) { e.printStackTrace(); } driver.close(); }

    参考文献:

    https://blog.csdn.net/l707268743/article/details/80942246

    https://www.cnblogs.com/sqy123/p/9057348.html#_labelTop

    https://www.cnblogs.com/lfri/p/10542797.html

    https://cloud.tencent.com/developer/ask/197199

    https://blog.csdn.net/bpz31456/article/details/80455708

    https://blog.csdn.net/MeGoodtoo/article/details/89042714

  • 相关阅读:
    Java & PHP RSA 互通密钥、签名、验签、加密、解密
    Spring Cloud:Security OAuth2 自定义异常响应
    Spring Cloud:统一异常处理
    Spring Cloud:多环境配置、eureka 安全认证、容器宿主机IP注册
    Hexo + GitEE 搭建、备份、恢复、多终端
    Spring:AOP面向切面编程
    如何找到适合自己的文档工具?
    比较好玩的工具类合集推荐!!!
    C++11 如何使代码仅执行一次?
    CMake 常用操作有哪些?
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/13454457.html
Copyright © 2011-2022 走看看