无头浏览器ChromeDriver安装,java 使用 driver 网页截图

Win环境

安装 chromedriver 下载地址:https://npm.taobao.org/mirrors/chromedriver/。

解压chromedriver.exe 文件到自己的目录

ubuntu环境

1、下载文件,通常在 Downloads 目录下:

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

2、安装应用:

sudo dpkg -i google-chrome-stable_current_amd64.deb

3、若因依赖问题安装失败,执行此命令修复依赖后,再次执行安装命令:

sudo apt install -fy

4、若修复依赖 apt-get安装包404 Error ,执行此命令更新后再执行修复依赖命令:

sudo apt-get update

 

google-chrome --version  查看版本

我的:Google Chrome 89.0.4389.90 

安装 chromedriver

 5、在chromedriver 下载 对应版本的安装包解压

unzip chromedriver_linux64.zip

6、移到/usr/bin/目录下

mv chromedriver /usr/bin/

7、测试

chromedriver

输出:

Starting ChromeDriver 89.0.4389.23 (61b08ee2c50024bab004e48d2b1b083cdbdac579-refs/branch-heads/4389@{#294}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

java使用 引入驱动依赖

        
            org.seleniumhq.selenium
            selenium-java
            2.45.0
        

配置 chromedriver

新建ChromeDriverConfig.class

@Configuration
public class ChromeDriverConfiguration {
    private static final Logger log = LoggerFactory.getLogger(ChromeDriverConfiguration.class);
    private static final String DRIVER_KEY = "webdriver.chrome.driver";

    @Autowired
    private SettingService settingService;

    @Bean
    public ChromeOptions buildChromeOptions(){
        String os = System.getProperty("os.name");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("headless");
        options.addArguments("--disable-gpu");
        options.addArguments("--allowed-ips");
        options.addArguments("--ignore-certificate-errors");
        String chromeDriverPath = "D:/chrome/chromedriver.exe"
        if(new File(chromeDriverPath).exists()) {
             log.error("chromedriver.exe not fund path:[{}]", chromeDriverPath);            
            throw new BeanInitializationException("The chromedriver.exe executable does not exist: " + chromeDriverPath);
        }
        System.setProperty(DRIVER_KEY, chromeDriverPath);
        log.info("ChromeDriverConfiguration load to chromedriver path:[{}]", chromeDriverPath);
        return options;
    }

    @Bean
    public ChromeDriver buildWebDriver(ChromeOptions options){
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();  //窗口最大化
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        return driver;
    }
}

使用:

    @Autowired
    private ChromeDriver chromeDriver;
    
    public File generatePreview(String webUrl) throws InterruptedException {
        if (StringUtils.isBlank(webUrl)) {
            throw new RuntimeException("无效的请求地址");
        }
        chromeDriver.get(webUrl);
        //等待网页加载完毕
        Thread.sleep(3000L);
        File previewFile = chromeDriver.getScreenshotAs(OutputType.FILE);
        //保存本地
        FileUtil.copy(previewFile, new File(    "D:" + File.separator + "image.png"),true);
        return previewFile;
    }

 

你可能感兴趣的:(google,chrome,chrome,driver)