(1)安装chrome vim /etc/yum.repos.d/google-chrome.repo 粘贴以下内容 ------------------ [google-chrome] name=google-chrome baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch enabled=1 gpgcheck=1 gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub ------------------ 然后安装 yum -y install google-chrome-stable 失败就用这个: yum -y install google-chrome-stable --nogpgcheck
祈祷中间不要出错吧
安装成功后,chrome在/usr/bin/google-chrome下, 注意,google-chrome是个可执行文件,不是目录
命令: /usr/bin/google-chrome -version 可以查看安装的版本,如Google Chrome 64.0.3282.119 ------- 搞不定的话,可以离线安装,参考 http://mytju.com/classcode/news_readNews.asp?newsID=1101
(2)下载chromeDriver https://chromedriver.storage.googleapis.com/index.html 注意看每个版本下的notes.txt,里面有chromeDriver和chrome的对应版本关系 ----------ChromeDriver v2.35 (2018-01-10)---------- Supports Chrome v62-64
----------ChromeDriver v2.34 (2017-12-10)---------- Supports Chrome v61-63
----------ChromeDriver v2.33 (2017-10-03)---------- Supports Chrome v60-62
----------ChromeDriver v2.32 (2017-08-30)---------- Supports Chrome v59-61
----------ChromeDriver v2.31 (2017-07-21)---------- Supports Chrome v58-60
----------ChromeDriver v2.30 (2017-06-07)---------- Supports Chrome v58-60
我用的脚本: ------------- mkdir -p /opt/webDriver cd /opt/webDriver wget https://chromedriver.storage.googleapis.com/2.35/chromedriver_linux64.zip unzip chromedriver_linux64.zip chmod 775 chromedriver -------------
这有google的说明: https://sites.google.com/a/chromium.org/chromedriver/getting-started
(3)Java代码
// 懒得写配置,写死路径 String driverPath = "D:\\chromedriver.exe";
// 判断系统 String os = System.getProperties().getProperty("os.name"); if (os != null && os.toLowerCase().indexOf("linux") > -1) { driverPath = "/opt/webDriver/chromedriver"; }
// 设置Driver路径 System.setProperty("webdriver.chrome.driver", driverPath);
// 创建一个Driver对象 ChromeDriverService service = ChromeDriverService .createDefaultService(); ChromeOptions options = new ChromeOptions(); options.addArguments("--headless", "--disable-gpu","--no-sandbox"); driver = new ChromeDriver(service, options);
好像就行了。
|