福音!Selenium使用WebDriverManager以后,再也不用被浏览器driver与浏览器版本不匹配的问题折磨了!
作者:mmseoamin日期:2024-02-06

福音!Selenium使用WebDriverManager以后,再也不用被浏览器driver与浏览器版本不匹配的问题折磨了!,第1张

相信做selenium自动化测试开发的同学会经常遇到类似的问题:

警告: The chromedriver version (114.0.5735.90) detected in PATH at D:\webdriver\chromedriver.exe might not be compatible with the detected chrome version (120.0.6099.110); currently, chromedriver 120.0.6099.109 is recommended for chrome 120.*, so it is advised to delete the driver in PATH and retry

上面问题的本质就是chromedriver与chrome版本不匹配,需要我们自己找到匹配的chromedriver与chrome!大家可以试试找chromedirver 120.0.6099.110 版本有多么的费劲,办小时能搞定都是快的!!!!

为了解决chromedriver与chrome版本自动匹配的问题,我们今天来讲讲WebDriverManager

WebDriverManager概念

大家都知道,在进行 Selenium 测试时,需要一个与各个浏览器相匹配的 driver,以便控制和操作浏览器。做过Selenium自动化测试的同学都知道,一旦浏览器升级(事实上好多浏览器都是自动升级的),我们就要手动下载对应的浏览器相匹配的 driver,这个过程非常讨厌!Webdriver manager 则为我们提供了一种简便的方式,可以自动检测所需浏览器的版本并下载相应的driver,帮我们减轻负担,同时也提高了测试的可靠性和可维护性。

WebDriverManager是一个开源的Java库,它执行Selenium WebDriver所需驱动程序的管理(即下载、设置和维护)(例如,chromedriver、geckodriver、edgedriver等)。以全自动的方式进行。此外,从版本5开始,WebDriverManager提供了其他相关功能,例如发现本地系统中安装的浏览器、构建WebDriver对象(例如ChromeDriver、FirefoxDriver、EdgeDriver等)、在Docker容器中无缝运行浏览器以及监控功能。官网:https://github.com/bonigarcia/webdrivermanager

如何使用WebDriverManager 非常简单,在pom配置如下:

    io.github.bonigarcia

    webdrivermanager

    5.6.2

    test

WebDriverManager应用实例

我们来做一个完整的实例

selenium使用最新版本4.16.1,

junit4使用最新版本 4.13.2,

webdrivermanager,5.6.2,

以上三者在maven中配置即可,后面的chrome 、jdk 以及maven 需要自己安装

chrome使用最新版本 120.0.6099.110(正式版本) (64 位),

jdk 11.0.12,

maven 3.8.2

项目的maven配置如下:


            org.seleniumhq.selenium
            selenium-java
            4.16.1


            io.github.bonigarcia
            webdrivermanager
            5.6.2
                test


        junit
        junit
        4.13.2
        test
 

selenium代码如下:

package com.my.demo;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.NoSuchElementException;
public class BaiduTestDemo {
	WebDriver driver;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
      //设置chromedirver,默认下载路径为C:\Users\Administrator\.cache\selenium\chromedriver
	  //是当前登录用户的\.cache\selenium\chromedriver路径
		 
       WebDriverManager.chromedriver().setup(); 
		
	}
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}
	@Test
	public void test() {
		driver =new ChromeDriver();
		String url = "http://www.baidu.com";
		driver.get(url);
		driver.manage().window().maximize();
		driver.findElement(By.id("kw")).sendKeys("selenium");
		driver.findElement(By.id("su")).click();
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		assertTrue(isElementPresent(By.partialLinkText("selenium"))); //验证是否有selenium 相关搜索链接
		
	    driver.quit();
	}
	
    private boolean isElementPresent(By by) {
    	try {	
    		driver.findElement(by);
    		return true;
    	}catch(NoSuchElementException e) {
    		return false;
    		
    	}
    	
    }
}

执行代码后,脚本正常执行,WebDriverManager 会自动下载driver匹配当前浏览器版本,非常的方便!大家可以下载maven工程源码,链接如下:

https://download.csdn.net/download/liwenxiang629/88652190

我的每一篇文章都希望帮助读者解决实际工作中遇到的问题!如果文章帮到了您,劳烦点赞、收藏、转发!您的鼓励是我不断更新文章最大的动力!