美文网首页
html截图

html截图

作者: AZZCS_0222 | 来源:发表于2019-12-26 18:21 被阅读0次

    selenium+ChromeDriver
    解决中文方块问题

    yum groupinstall "X Window System" -y  
    
    yum -y groupinstall chinese-support  
    
    yum -y groupinstall Fonts  
    

    实现代码

    package com.doumob.html2image.utils;
    
    import com.doumob.html2image.dto.IndexDTO;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.io.FileUtils;
    import org.openqa.selenium.*;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @Author: wangzhiguo
     * @Date: 2019/11/18 14:02
     */
    @Slf4j
    public class ChromeUtils {
    
        private static String chromeDriverPathLinux="/usr/local/bin/";
        private static String chromeDriverPathWindows="D:\\html_to_image\\chromedriver_win32\\";
    
        public static void chromeToImage(List<IndexDTO> dtos) throws IOException, InterruptedException {
            String chromeDriverPath = "";
            if(System.getProperty("os.name").indexOf("Windows") == -1){
                chromeDriverPath=chromeDriverPathLinux;
            }else {
                chromeDriverPath=chromeDriverPathWindows;
    
            }
            ChromeOptions chromeOptions = initOptions(chromeDriverPath);
            WebDriver driver = new ChromeDriver(chromeOptions);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
            String destPath = chromeDriverPath+"images";
            File file = new File(destPath);
            File parent = file.getParentFile();
            if(!parent.exists()){
                parent.mkdirs();
            }
            for(IndexDTO dto:dtos){
                if(dto.getUrlPath().startsWith("http")){
                    System.out.println(dto.getId()+" ------------- "+dto.getUrlPath());
                    driver.manage().window().setSize(new Dimension(375, 621));
                    driver.get(dto.getUrlPath());
                    TimeUnit.SECONDS.sleep(2);
                    JavascriptExecutor driverJs= ((JavascriptExecutor) driver);
                    Object oWidth = driverJs.executeScript("return Math.max(document.body.scrollWidth==null?0:document.body.scrollWidth, document.body.offsetWidth==null?0:document.body.offsetWidth, document.documentElement.clientWidth==null?0:document.documentElement.clientWidth, document.documentElement.scrollWidth==null?0:document.documentElement.scrollWidth==null, document.documentElement.offsetWidth==null?0:document.documentElement.offsetWidth);");
                    Object oHeight = driverJs.executeScript("return Math.max(document.body.scrollHeight==null?0:document.body.scrollHeight, document.body.offsetHeight==null?0:document.body.offsetHeight, document.documentElement.clientHeight==null?0:document.documentElement.clientHeight, document.documentElement.scrollHeight==null?0:document.documentElement.scrollHeight, document.documentElement.offsetHeight==null?0:document.documentElement.offsetHeight);");
                    Long width = (Long) oWidth;
                    Long height = (Long)oHeight;
                    System.out.println(dto.getId()+"-----"+width+"-----"+height);
                    driver.manage().window().setSize(new Dimension(width.intValue(), height.intValue()));
                    try {
                        WebElement element = driver.findElement(By.tagName("body"));
                        File screenshot = element.getScreenshotAs(OutputType.FILE);
                        FileUtils.copyFile(screenshot, new File(destPath + File.separator + dto.getId() + ".png"));
                    }catch (Exception e){
                        log.error("生成图片失败,id:{},url:{}",dto.getId(),dto.getUrlPath());
                    }
                }
            }
            driver.quit();
        }
    
        private static ChromeOptions initOptions(String chromeDriverPath) {
            if(System.getProperty("os.name").indexOf("Windows") == -1){
                chromeDriverPath=chromeDriverPath+"chromedriver";
            }else {
                chromeDriverPath=chromeDriverPath+"chromedriver.exe";
    
            }
            System.out.println(chromeDriverPath);
            System.setProperty("webdriver.chrome.driver", chromeDriverPath);
            ChromeOptions options = new ChromeOptions();
            List<String> arguments = new ArrayList<>();
            arguments.add("--headless");
            arguments.add("--disable-gpu");
            arguments.add("--ignore-certificate-errors");
            arguments.add("--disable-features=VizDisplayCompositor");
            arguments.add("--silent");
            arguments.add("--incognito");
            arguments.add("enable-automation");
            arguments.add("--no-sandbox");
            arguments.add("--window-size=774,1393");
            arguments.add("--disable-extensions");
            arguments.add("--dns-prefetch-disable");
            options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
            options.addArguments(arguments);
    
            return options;
        }
    
        public static void main(String[] args) throws IOException, InterruptedException {
            List<IndexDTO> dtos = new ArrayList<>();
            dtos.add(new IndexDTO(6486L,"https://nobug.doumob.cn/static/3dzhuanpan5/index.html"));
            chromeToImage(dtos);
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:html截图

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