Selenium 网站转图片


1. 需求

公司的公众号会定期发布文章,小程序需要同步发布,也就是要编辑两次,为了节约时间,小程序改为以 “公众号文章长截图” 的形式发布。

2. 代码实现

  • pom.xml 添加依赖
<!-- Selenium -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

<!-- Commons IO -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.13.0</version>
</dependency>
  • DemoApplication.java
    package com.example.demo;
    
    import org.openqa.selenium.JavascriptExecutor;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
    
            // ChromeDriver路径(需根据实际情况修改)
            System.setProperty("webdriver.chrome.driver", "C:/Users/liu/Downloads/chromedriver-win64/chromedriver.exe");
    
            // 配置Chrome选项
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--headless"); // 无头模式
            options.addArguments("--disable-gpu");
            options.addArguments("--window-size=1920,1080");
            options.addArguments("--remote-allow-origins=*");
            options.addArguments("--hide-scrollbars");
    
            WebDriver driver = new ChromeDriver(options);
    
            try {
                // 公众号文章地址
                String url = "https://mp.weixin.qq.com/s/a9dy5N4pn1Ap1f-JnUwUSg";
    
                // 访问目标页面
                driver.get(url);
    
                // 等待页面加载(可根据实际情况调整)
                Thread.sleep(5000);
    
                // 生成网页预览图
                 takeScreenshot(driver, 1920, "web_preview.png");
                // 生成手机预览图
                 takeScreenshot(driver, 750, "mobile_preview.png");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                driver.quit();
            }
    
            SpringApplication.run(DemoApplication.class, args);
        }
    
        // 截屏
        private static void takeScreenshot(WebDriver driver, int width, String fileName) throws Exception {
            // 调整浏览器窗口尺寸
            Long height = (Long) ((JavascriptExecutor)driver).executeScript("return document.documentElement.scrollHeight");
            driver.manage().window().setSize(new org.openqa.selenium.Dimension(width, height.intValue()));
    
            // 等待页面重新布局
            Thread.sleep(2000);
    
            // 截图并保存
            File srcFile = ((ChromeDriver) driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(srcFile, new File(fileName));
            System.out.println("已生成截图:" + fileName);
        }
    }
    

3. 效果测试图

mobile_preview


文章作者: Alex
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Alex !
  目录