文档列表
- Selenium: https://www.selenium.dev/documentation/
- WebDriverManager: https://bonigarcia.dev/webdrivermanager/
基本设置
- 依赖设置:
<!-- WebDriver, ChromeDriver 相关引用 -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
<!-- WebDriverManager 相关引用 -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.2.1</version>
<scope>compile</scope>
</dependency>
<!-- 测试模块 相关引用 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
- 安装浏览器驱动:
Browser |
Supported OS |
Maintained by |
Download |
Issue Tracker |
Chromium/Chrome |
Windows/macOS/Linux |
Google |
Downloads |
Issues |
Firefox |
Windows/macOS/Linux |
Mozilla |
Downloads |
Issues |
Edge |
Windows/macOS |
Microsoft |
Downloads |
Issues |
Internet Explorer |
Windows |
Selenium Project |
Downloads |
Issues |
Safari |
macOS High Sierra and newer |
Apple |
Built in |
Issues |
- 将
geckodriver.exe
进行解压并放置到PATH
文件夹内
- 运行测试脚本
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) {
// 如果没有将驱动放置到PATH中, 则需要使用System.setProperty设置地址
// System.setProperty("webdriver.chrome.driver","PATH TO geckodriver.exe");
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.quit();
}
}
第一个脚本
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
public class Main {
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
WebDriver driver;
driver = new ChromeDriver(); // 实例化驱动程序, 并开启一个 Chrome 进程
driver.get("https://google.com"); // 打开一个网站
String title = driver.getTitle(); // 直接获取网站名称
Assertions.assertEquals("Google", title); // 判断网站名称是否符合预期
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); // 手动等待 500 ms 以令javascript完成加载
WebElement searchBox = driver.findElement(By.name("q")); // 通过tag获取对象
WebElement searchButton = driver.findElement(By.name("btnK"));
searchBox.sendKeys("Selenium");
searchButton.click();
// 点击完成后会发生页面的更新
// 如果直接进行输出会触发: org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException
// System.out.println(searchBox.getAttribute("value"));
searchBox = driver.findElement(By.name("q")); // 因为发生页面更新所以之前的element失效
String value = searchBox.getAttribute("value");
Assertions.assertEquals("Selenium", value);
driver.quit();
}
}
与功能相关的重要文档链接
- 浏览器网页浏览, 网页回退, 刷新, 前进等功能: https://www.selenium.dev/documentation/webdriver/browser/navigation/
- 对于弹出提示的处理: https://www.selenium.dev/documentation/webdriver/browser/alerts/
- cookie操作: https://www.selenium.dev/documentation/webdriver/browser/cookies/
- 窗口与选项卡操作: https://www.selenium.dev/documentation/webdriver/browser/windows/
- html元素选择: https://www.selenium.dev/documentation/webdriver/elements/
- JS等待条件: https://www.selenium.dev/documentation/webdriver/waits/
- 浏览器底层 API 操作(用于进行网页打印等操作): https://www.selenium.dev/documentation/webdriver/actions_api/
其他资源:
- selenium-ide 浏览器插件用于使用selenium语句录制操作步骤: https://www.selenium.dev/selenium-ide/