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();
    }
}
相关推荐
NE_STOP2 小时前
Vide Coding--AI编程工具的选择
java
码云数智-园园3 小时前
C++20 Modules 模块详解
java·开发语言·spring
程序员黑豆3 小时前
JDK 下载安装与配置详细教程
java·前端·ai编程
小宇宙Zz3 小时前
Maven依赖冲突
java·服务器·maven
swordbob3 小时前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
咖啡八杯4 小时前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
十五喵源码网4 小时前
基于springboot2+vue2的租房管理系统
java·毕业设计·springboot·论文笔记
摇滚侠4 小时前
IDEA 创建 Java 项目 手动整合 SSM 框架
java·ide·intellij-idea
源分享4 小时前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Flittly4 小时前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring