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();
    }
}
相关推荐
程序员小假23 分钟前
我们来说一下 b+ 树与 b 树的区别
java·后端
Meepo_haha1 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
sheji34161 小时前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
木井巳1 小时前
【递归算法】子集
java·算法·leetcode·决策树·深度优先
行百里er2 小时前
优雅应对异常,从“try-catch堆砌”到“设计驱动”
java·后端·代码规范
ms_27_data_develop2 小时前
Java枚举类、异常、常用类
java·开发语言
xiaohe072 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
代码飞天3 小时前
wireshark的高级使用
android·java·wireshark
gechunlian883 小时前
Spring Boot中的404错误:原因、影响及处理策略
java·spring boot·后端