无头浏览器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使用 引入驱动依赖

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

配置 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;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值