基于springboot2.x集成selenium3.141.59

本文介绍了如何在SpringBoot 2.x项目中集成Selenium 3.141.59,用于自动化浏览器测试。重点强调了Chrome和Firefox驱动与浏览器版本的对应关系,并提供了详细的环境配置,包括JDK、Chrome和Firefox的版本。此外,展示了如何设置无界面静默启动,以及提供国内镜像下载地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

本人也是初次接触selenium,被版本坑了好久总结几点避免入坑的注意点,
仔细按照文中的版本安装配置保证一遍过。
不要太在意浏览器版本过低有什么影响的,只要能满足你的需求就可以了。
---->重要的话说三遍<-----
1. 不管是chrome还是firefox最重要的就是他们驱动与浏览器之间的版本对应关系。
2. 不管是chrome还是firefox最重要的就是他们驱动与浏览器之间的版本对应关系。
3. 不管是chrome还是firefox最重要的就是他们驱动与浏览器之间的版本对应关系。

windows环境参考

jdk: 1.8.0_171
## 谷歌浏览器
chrome:
	chromedriver.exe --> 80.0.3987.16 
	google chrome --> 80.0.3987.132 (64位) 
	下载地址: https://www.chromedownloads.net/chrome64win-stable/978.html
	selenium-java --> 3.141.59
## 火狐浏览器	
firefox:
	geckodriver.exe --> v0.21.0
	firefox --> 57.0b8 (64 位) 
	下载地址:http://ftp.mozilla.org/pub/firefox/releases/57.0/win64/zh-CN/
	selenium-java --> 3.141.59

核心代码

github代码地址:https://github.com/gitwangkui/springboot-selenium.git

pom.xml

<dependencies>
		<!-- selenium -->
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>3.141.59</version>
		</dependency>
		<!-- 此jar包容易冲突或版本不符合,暂时放在这里方便更换版本号-->
		<!-- <dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>25.0-jre</version>
		</dependency> -->
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>RELEASE</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>

ChromeDemo001_80.java

说明: 该java文件命名是为了区分chrome的版本号,可以忽略重新命名,详情可以查看github源码。

import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/**
 * 
 * @Description: chromedriver-80.0.3987.16 , Firefox-80.0.3987.132(64位) , Selenium-3.141.59
 * @author: uwank171 
 * @date: 2020年10月29日 下午2:07:25 
 *
 */
public class ChromeDemo001_80 {
	private WebDriver driver;
	private Map<String, Object> vars;
	JavascriptExecutor js;

	@Before
	public void setUp() {
		System.out.println("========= 开始  ==========");
		String chrome_driver_path = ".\\src\\main\\resources\\driver\\chrome_80_0_3987_16\\chromedriver.exe";
		System.setProperty("webdriver.chrome.driver", chrome_driver_path);
		// 无界面静默启动(不启动浏览器)
		ChromeOptions options = new ChromeOptions();
		options.setHeadless(true);
		driver = new ChromeDriver(options);
		//常规启动			
		//driver = new ChromeDriver();
		js = (JavascriptExecutor) driver;
		vars = new HashMap<String, Object>();
	}

	@After
	public void tearDown() throws Exception {
		Thread.sleep(2000);
		System.out.println("========= 关闭 ============");
		driver.quit();
	}

	@Test
	public void demo001() {
		try {
			long l = System.currentTimeMillis();
			driver.get("http://www.baidu.com");
			driver.findElement(By.id("kw")).sendKeys("selenium");
			driver.findElement(By.id("su")).click();
			/**
			 * Thread.sleep(1000); 不加这个直接执行的可能会报错,找不到元素。
			 * 猜想可能是页面代码没有加载出来,而程序执行到下一步所以无法找到该元素。
			 */
			//Thread.sleep(1000);
			//driver.findElement(By.xpath("//*[@id=\"1\"]/h3/a[1]")).click();
			System.out.println("==耗时:" + (System.currentTimeMillis() - l - 1000) + " 毫秒");
		} catch (Exception e) {
			System.err.println("\n==== " + e);
		}
	}
}

FirefoxDemo001_57.java

说明: 该java文件命名是为了区分firefox的版本号,可以忽略重新命名,详情可以查看github源码。

import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
/**
 * 
 * @Description: geckodriver-v0.21.0 , Firefox 57 , Selenium -3.141.59
 * @author: uwank171 
 * @date: 2020年10月29日 下午2:06:39 
 *
 */
public class FirefoxDemo001_57 {
	private WebDriver driver;
	private Map<String, Object> vars;
	JavascriptExecutor js;

	@Before
	public void setUp() {
		System.out.println("========= 开始  ==========");
		String firefox_path = "C:\\Program Files\\Mozilla Firefox57\\firefox.exe";
		String firefox_driver_path = ".\\src\\main\\resources\\driver\\firefox_0_21_0\\geckodriver.exe";
		// 火狐的安装位置
		System.setProperty("webdriver.firefox.bin", firefox_path);
		// 加载驱动 webdriver.gecko.driver --> 47以上版本使用
		System.setProperty("webdriver.gecko.driver", firefox_driver_path);
		
		// 无界面启动(不启动浏览器)
		FirefoxOptions options = new FirefoxOptions();
		options.setHeadless(true);
		driver = new FirefoxDriver(options);
		//driver = new FirefoxDriver();
		js = (JavascriptExecutor) driver;
		vars = new HashMap<String, Object>();
	}

	@After
	public void tearDown() throws Exception {
		Thread.sleep(2000);
		System.out.println("========= 关闭 ============");
		driver.quit();
	}

	@Test
	public void demo001() {
		try {
			long l = System.currentTimeMillis();
			driver.get("http://www.baidu.com");
			driver.findElement(By.id("kw")).sendKeys("selenium");
			driver.findElement(By.id("su")).click();
			/**
			 * Thread.sleep(1000); 不加这个直接执行的可能会报错,找不到元素。
			 * 猜想可能是页面代码没有加载出来,而程序执行到下一步所以无法找到该元素。
			 */
			//Thread.sleep(1000);
			//driver.findElement(By.xpath("//*[@id=\"1\"]/h3/a[1]")).click();
			System.out.println("==耗时:" + (System.currentTimeMillis() - l - 1000) + " 毫秒");
		} catch (Exception e) {
			System.err.println("\n==== " + e);
		}
	}
}

设置无界面静默启动

// chrome 无界面启动(不启动浏览器)
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
driver = new ChromeDriver(options);

// firefox无界面启动(不启动浏览器)
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
driver = new FirefoxDriver(options);

国内镜像地址

如下地址可供参考
chromedriver国内镜像地址: https://npm.taobao.org/mirrors/chromedriver/
chrome国内下载地址:https://www.chromedownloads.net/chrome64win/
geckodriver的github地址:https://github.com/mozilla/geckodriver/releases
firefox浏览器各版本下载地址:http://ftp.mozilla.org/pub/firefox/releases/

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值