首先下载chromedriver,chromedriver需要跟chrom浏览器版本匹配:
国内: http://npm.taobao.org/mirrors/chromedriver/
国外: http://chromedriver.storage.googleapis.com/index.html
我下载的是73.0.3683.68版本的chromedriver_linux64.zip
1. 安装chromeium:
# 安装最新的chrome浏览器
[root@izwz97vf52xuyjdkqfsw1oz ~]# yum install -y chromium
2. 安装chromedriver:
# 安装unzip
[root@izwz97vf52xuyjdkqfsw1oz ~]# yum install -y unzip zip
# 将下载好的chromedriver解压
[root@izwz97vf52xuyjdkqfsw1oz ~]# unzip chromedriver_linux64.zip
# 将解压后的chromedriver移至/usr/bin/
[root@izwz97vf52xuyjdkqfsw1oz ~]# mv chromedriver /usr/bin
3. 更改权限(重要):
[root@izwz97vf52xuyjdkqfsw1oz ~]# chmod +x chromedriver
4. 查看chromedriver是否安装成功:
[root@izwz97vf52xuyjdkqfsw1oz ~]# chromedriver --version
5.Java代码:
public static WebDriver createChromeDriver() {
// 创建chrome浏览器驱动,无头模式
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("window-size=1920x3000");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--hide-scrollbars");
chromeOptions.addArguments("blink-settings=imagesEnabled=false");
chromeOptions.addArguments("--headless");
return new ChromeDriver(chromeOptions);
}