CentOS下配置 java selenium chrome环境
1. 安装chrome (版本要和chrome webdriver版本对应)
1.1下载安装包:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
1.2安装
由于chrome还依赖其他的组件,所以可以采用和依赖组件一起安装的方式。
执行下面的命令:
yum localinstall google-chrome-stable_current_x86_64.rpm
安装完成后,执行命令,查看版本:
1.3查看版本号
google-chrome --version
Google Chrome {xxx版本}
2.下载对应的 chrome webdriver
下载地址:
https://registry.npmmirror.com/binary.html?path=chromedriver/{版本号}/
下载chromedriver_linux64.zip。
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);
TakesScreenshot screenshot = (TakesScreenshot) webDriver;
OutputType<File> OutputType= screenshot.getScreenshotAs(OutputType.FILE);
如有错误欢迎指正