1. 安装chrome
下载安装包:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
由于chrome还依赖其他的组件,所以可以采用和依赖组件一起安装的方式。
执行下面的命令:
yum localinstall google-chrome-stable_current_x86_64.rpm
安装完成后,执行命令,查看版本:
# google-chrome --version
Google Chrome 100.0.4896.127
2.下载对应的 chrome webdriver
下载地址:
https://registry.npmmirror.com/binary.html?path=chromedriver/100.0.4896.60/
下载chromedriver_linux64.zip。
解压,记录目录地址 ,例:/usr/local/chrome/chromedriver
3. pom.xml依赖配置
selenium-java版本不宜太高
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
4.代码参数配置
// 设置 chromedirver 的存放位置
System.getProperties().setProperty("webdriver.chrome.driver", "/usr/local/chrome/chromedriver");
// 设置浏览器参数
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--no-sandbox");//禁用沙箱
chromeOptions.addArguments("--disable-dev-shm-usage");//禁用开发者shm
chromeOptions.addArguments("--headless"); //无头浏览器,这样不会打开浏览器窗口
webDriver = new ChromeDriver(chromeOptions);
String uri = "http://www.baidu.com";
webDriver.get(uri);
版权声明:本文为xingnang2008原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。