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();
    }
}
相关推荐
CRMEB系统商城6 小时前
开源自建还是SaaS订阅?算一笔3年经济账
java·大数据·开发语言·开源
keyipatience7 小时前
线程栈与TLS和线程互斥
java·linux·服务器·开发语言·ubuntu
weixin_408099677 小时前
2026 图片去水印 API 接口完全指南:一键去除图片水印(附 Python/Java/PHP/C# 示例)
java·python·php·图片处理·api调用·图片去水印·石榴智能
钱六两8 小时前
#3、SpringAI 接入deepSeek大模型(喂饭版)
java·spring boot·ai编程
null_179 小时前
IntelliJ IDEA 极致流畅配置方案:Ultra 9 285K + 64GB 内存实测
java·ide·intellij-idea
Herbert_hwt9 小时前
建立Java程序开发
java·开发语言
好好沉淀9 小时前
@ExcelIgnoreUnannotated 和 @AutoMapper 详解
java
愚公移码9 小时前
蓝凌EKP18产品:流程虚拟机(PVM)
java·开发语言·前端
驰骋工作流10 小时前
流程引擎BPM设计之:流程消息
java·工作流引擎·bpm·jflow·ccflow
dogstarhuang10 小时前
从 0 到 1 搭建可收费的 API 开放平台(实战)
java·架构·api