Chrome 浏览器 + IDEA
1:查看Chrome浏览器版本:
2:下载驱动:Chrome 驱动
我的 Chrome 版本是 121(大版本)
找到对应的下载,Windows 的就下载 win32 那个。
没有自己版本的
点击 Stable 进去,找到对应版本,复制链接即可下载。
3:添加到 java 环境中来:
4:新建一个项目,在 pom.xml 添加依赖:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
代码复制进去,可能会报红,需要点击右边加载,等待下载刷新后就不会报红了。
5:编写测试代码:
package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Main {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver = new ChromeDriver(options);
webDriver.get("https://www.baidu.com");
}
}
运行结果如下: