版本说明:
因为之前使用过selenium的版本为3.141.59,geckodriver的版本为0.31.0,在服务器上准备通过安装包的方式安装firefox时依赖问题严重,因此通过yum源的方式来安装,同时通过以下命令来把需要的依赖下载好,方便后面没有网络环境时安装。
yum -y install --downloadonly --downloaddir=/apphome/bers/simDisk/driver/rpms firefox
贴一下maven依赖:
<dependency>
<groupId>org.openqa.selenium.client-drivers</groupId>
<artifactId>selenium-java-client-driver</artifactId>
<version>0.9.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-api -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>4.2.2</version>
</dependency>
说明:
centos7使用yum安装的最新版firefox版本为 102.8.esr,因此修改selenium的版本为4.2.2(4.1.2的中央仓库下载下来的jar包是空的),对应selenium-api的版本也是4.2.2。
java调用示例:
FirefoxOptions profile = new FirefoxOptions();
String url="www.baidu.com";
String sys=System.getProperty("os.name");
if (sys.toLowerCase().startsWith("win")){
System.setProperty("webdriver.gecko.driver", "network-simDisk/src/main/resources/driver/geckodriver.exe");
}else {
//linux系统可以将geckodriver文件拷贝到/usr/local/bin/目录下,无需单独配置路径
System.out.println("linux");
}
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/dev/null");
profile.setHeadless(true);
FirefoxDriver driver = new FirefoxDriver(profile);
//最大化窗口
driver.manage().window().maximize();
//设置隐性等待时间
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.get(url);
遇到的问题:
1.依赖问题,经查看官网说明,需要如下几个依赖,大家可以在yum安装时保留安装包以备后续使用:
glibc 2.17 or higher
GTK+ 3.14 or higher
libstdc++ 4.8.1 or higher
X.Org 1.0 or higher
2.启动项目时报如下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'callBackController': Unsatisfied dependency expressed through field 'service'; nested ex
ception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'diskAuthTokenServiceImpl' defined in URL [jar:file:/apphome/bers/simDisk/network-simDi
sk-1.0.0.jar!/BOOT-INF/classes!/com/webimation/network/service/impl/DiskAuthTokenServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInst
antiationException: Failed to instantiate [com.webimation.network.service.impl.DiskAuthTokenServiceImpl]: Constructor threw exception; nested exception is org.openqa.selenium.WebDriverE
xception: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:10730
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
主要问题有两个,一个是geckodriver和firefox版本问题,这个问题会导致Build info后面的值都是unknown,第二个是/etc/hosts文件中没有写localhost配置,主要问题是第二个,因为这个问题研究一整天,最后是这个问题真是醉了。
最后补充一点,在spring项目中添加关闭前停止所有geckodirver的操作,不然会导致重复运行多个geckodriver导致资源浪费;推荐整个项目使用单例模式来实现同一个全局变量FirefoxDriver,然后打开不同tab标签后记录每个标签对应的handlerId,通过DisposableBean来实现项目关闭前停止geckodriver。
备注:关闭项目时使用kill -2 不要用kill -9 切记
代码如下:
项目停止前触发代码
@Configuration
@Slf4j
public class ApplicationService implements DisposableBean{
private FirefoxOptUtil firefoxOptUtil= FirefoxOptUtil.getInstance();
@Override
public void destroy() throws Exception {
log.info("执行平台登出");
firefoxOptUtil.getDriver().quit();
}
}
FireFox单例
package com.webimation.network.util;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import java.util.concurrent.TimeUnit;
public class FirefoxOptUtil {
public static FirefoxOptUtil instance;
private static final String sys=System.getProperty("os.name");
private FirefoxDriver driver;
public static synchronized FirefoxOptUtil getInstance(){
if (instance==null){
instance=new FirefoxOptUtil();
}
return instance;
}
private FirefoxOptUtil(){
driver=getdriver();
}
/*
* 返回火狐无头浏览器driver
* */
private FirefoxDriver getdriver() {
FirefoxOptions profile = new FirefoxOptions();
if (sys.toLowerCase().startsWith("win")){
System.out.println("windows");
System.setProperty("webdriver.gecko.driver", "network-simDisk/src/main/resources/driver/geckodriver.exe");
}else {
System.out.println("linux");
}
//记录geckodriver启动进程信息
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/apphome/bers/simDisk/driver/log");
profile.setHeadless(true);
this.driver = new FirefoxDriver(profile);
//最大化窗口
this.driver.manage().window().maximize();
//设置隐性等待时间
this.driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
return this.driver;
}
public FirefoxDriver getDriver() {
return this.driver;
}
}