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();
    }
}
相关推荐
2501_9445215912 小时前
Deepmerge 跨端三方库仓颉适配全流程
java·python·eclipse·github
南归北隐13 小时前
Spring AI Alibaba Graph框架实现MCP数据库查询实战
java·后端·spring ai·mcp·spring ai mcp
古法老登瞎折腾13 小时前
大数据智能运维实战(九):让 AI 查日志——找到异常,也保留上下文
java·大数据·运维·人工智能·spring ai·mcp
ai大模型-探索者13 小时前
Function Call与MCP(区别、联系与实战)
java·服务器·网络
略略略咯咯14 小时前
啊啊啊啊啊
java·服务器·数据库
SL_staff14 小时前
运维救火实战:钉钉API变更致薪资计算中断,如何用可视化逻辑引擎5分钟热修复
java·开源·全栈
猫猫.15314 小时前
HashTable、HashMap、ConcurrentHashMap 核心区别总结
java·开发语言·java-ee
郭小李15 小时前
spring mvc 工作流程
java·spring·mvc
SL_staff15 小时前
BI大屏嵌入MES的登录态难题:JVS统一账号体系实战解法
java·数据分析·数据可视化
乌暮15 小时前
综合案例:宠物电商系统
java·开发语言·spring·mvc·restful·1024程序员节