前提
安装好java环境
有idea
创建项目
1. 选择 gradle groovy
2.首先关闭gradle wrapper的下载(网速好可以直接下载)
配置 gradle 环境
i. 如果下载gradle wrapper超时,可以修改为国内镜像
1 2 3 4 5 6 distributionBase =GRADLE_USER_HOME distributionPath =wrapper/dists distributionUrl =https://mirrors.cloud.tencent.com/gradle/gradle-7.4-bin.zip zipStoreBase =GRADLE_USER_HOME zipStorePath =wrapper/dists
ii. build.gradle 文件替换成如下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 plugins { id 'java' } group 'org.example' version '1.0-SNAPSHOT' compileJava.options .encoding = 'UTF-8' compileTestJava.options .encoding = 'UTF-8' repositories { mavenLocal() maven { url 'https://maven.aliyun.com/repository/public/' } maven { url 'https://maven.aliyun.com/repository/spring/' } maven { url 'https://maven.aliyun.com/repository/google/' } maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' } maven { url 'https://maven.aliyun.com/repository/spring-plugin/' } maven { url 'https://maven.aliyun.com/repository/grails-core/' } maven { url 'https://maven.aliyun.com/repository/apache-snapshots/' } mavenCentral() } dependencies { testImplementation 'junit:junit:4.13.2' implementation 'org.seleniumhq.selenium:selenium-edge-driver:4.12.1' implementation 'org.seleniumhq.selenium:selenium-remote-driver:4.12.1' implementation 'org.seleniumhq.selenium:selenium-support:4.12.1' implementation 'org.seleniumhq.selenium:selenium-remote-driver:4.12.1' }
iii. 等待依赖下载完成,即可开始编码
测试驱动
i. 在 src/test/java 测试类
ii. Case1中填入如下的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import org.junit.Test;import org.openqa.selenium.WebDriver;import org.openqa.selenium.edge.EdgeDriver;public class Case1 { @Test public void test1 () throws InterruptedException { WebDriver webDriver = new EdgeDriver (); webDriver.get("https://www.bing.com" ); Thread.sleep(1500L ); webDriver.quit(); } }
iii. 运行测试代码
iv. 不难发现控制台有警告
1 2 3 警告: Unable to find CDP implementation matching 123 四月 13, 2024 10:43:20 下午 org.openqa.selenium.chromium.ChromiumDriver lambda$new$5 警告: Unable to find version of CDP to use for 123.0.2420.97. You may need to include a dependency on a specific version of the CDP using something similar to `org.seleniumhq.selenium:selenium-devtools-v86:4.12.1` where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's.
selenium库网页
可以按照这个网站提供的版本对应升级(修改build.gradle中的版本)