centos7+selenium+geckodriver+firefox+springboot最新配置及部分问题

因某些原因需要通过浏览器来访问一个网站,因此选用selenium+geckodriver来解决,这里挑选两个主要问题来记录说明,如有其他问题可以在评论里留言讨论解决

版本说明:
因为之前使用过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;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值