windows Java selenium chrome 设置

文档列表

  1. Selenium: https://www.selenium.dev/documentation/
  2. WebDriverManager: https://bonigarcia.dev/webdrivermanager/

基本设置

  1. 依赖设置:
<!--    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>
  1. 安装浏览器驱动:
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
  1. geckodriver.exe进行解压并放置到PATH文件夹内
  2. 运行测试脚本
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();
    }
}

与功能相关的重要文档链接

  1. 浏览器网页浏览, 网页回退, 刷新, 前进等功能: https://www.selenium.dev/documentation/webdriver/browser/navigation/
  2. 对于弹出提示的处理: https://www.selenium.dev/documentation/webdriver/browser/alerts/
  3. cookie操作: https://www.selenium.dev/documentation/webdriver/browser/cookies/
  4. 窗口与选项卡操作: https://www.selenium.dev/documentation/webdriver/browser/windows/
  5. html元素选择: https://www.selenium.dev/documentation/webdriver/elements/
  6. JS等待条件: https://www.selenium.dev/documentation/webdriver/waits/
  7. 浏览器底层 API 操作(用于进行网页打印等操作): https://www.selenium.dev/documentation/webdriver/actions_api/

其他资源:

  1. selenium-ide 浏览器插件用于使用selenium语句录制操作步骤: https://www.selenium.dev/selenium-ide/
posted @ 2022-07-16 20:39  NoobSir  阅读(208)  评论(0编辑  收藏  举报