java的gradle,maven工程中使用selenium

一、下载selenium库

(1)gradle工程

工程中会有一个build.gradle.kts的文件,这个文件可以定制 Gradle 的行为

在文件中添加下面代码,然后sync

bash 复制代码
//    implementation ("org.seleniumhq.selenium:selenium-java:4.19.1") # 下载的依赖在运行时使用
    testImplementation ("org.seleniumhq.selenium:selenium-java:4.19.1") # 下载的依赖在测试时使用

注:gradle工程的基本上会有如下的目录结构

├─main

│ ├─java

│ │ └─example

│ └─resources

└─test

├─java

│ └─example

└─resources

如果想要在main目录下使用,则使用implementation方式导入,如果想要在test目录下使用,则使用testImplementation方式导入

(2) maven 工程

工程的根目录下有一个pom.xml的文件

在pom.xml文件中添加下面代码,然后sync

java 复制代码
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.19.1</version>
        </dependency>
    </dependencies>

二、selenium demo代码

java 复制代码
package example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class test_demo {
    public static void main(String[] args) {
        // 设置 Chrome WebDriver 路径(根据实际情况设置)
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // 打开网页
        driver.get("https://www.baidu.com/");

//        driver.quit();
    }
}
相关推荐
似璟如你6 小时前
Java 开发者的 Go 语法基础:从 0 开始快速上手 Go
java·开发语言·后端·golang·go·编程语言
zzz_23687 小时前
TencentDB-Agent-Memory 深度解析:让多个 Agent 共享项目经验的记忆中枢
java·开发语言·jvm·人工智能·agent·memory·tencent db
vx-程序开发7 小时前
django医院预约挂号系统---附源码23353
java·javascript·spring boot·python·eclipse·django·php
金斗潼关7 小时前
ysoserial的使用
java
ruleslol7 小时前
volatile 关键字
java
lilian2338 小时前
Harmony os 技术实战|拼豆制图10:把取消、解析失败和保存失败写成可恢复状态机
java·javascript·华为·harmonyos
whn19778 小时前
达梦连接串JDBC测试程序
java·数据库
还是鼠鼠8 小时前
Spring AI ChatClient详解:创建第一个AI客户端
java·springboot·spring ai·chatclient
vHelios8 小时前
【电商项目】测试 授权功能 遇到的问题与解决方案
java·spring boot·sql
2401_894915539 小时前
GEO 定位优化源码搭建常见报错排查:数据库、伪静态、接口调试
java·数据库·网络协议·tcp/ip·spring·unity