Selenium 4.* 获取网页token

环境:

复制代码
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>${selenium.version}</version>
</dependency>

java 版本 11

chromeDriver
下载地址

我使用的版本:

https://storage.googleapis.com/chrome-for-testing-public/129.0.6668.58/win64/chromedriver-win64.zip

需要安装chrome浏览器

chrome版本:

129.0.6668.58以及129.0.6668.59

重点:chromeDriver和Chrome版本必须对应

也使用过webdrivermanager来控制版本,感觉不好用

代码:

java 复制代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.time.Duration;

/**
 * @author :gxg
 * @date :2024/9/19 15:55
 * @description:
 */
public class SeleniumTest {
    public static void main(String[] args) {
        // 设置 ChromeDriver 路径
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\test\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe"); // Windows

        ChromeOptions options = new ChromeOptions();
//        options.addArguments("--headless");// 如需无头模式,取消此行注释

        WebDriver driver = new ChromeDriver(options);

        driver.get("https://www.selenium.dev/selenium/web/web-form.html");

        driver.getTitle();

        driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));

        WebElement textBox = driver.findElement(By.name("my-text"));
        WebElement submitButton = driver.findElement(By.cssSelector("button"));

        textBox.sendKeys("Selenium");
        submitButton.click();
        // 以上是官网demo,可以改写成自己的登录逻辑
        WebElement submitButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("className")));
        submitButton.click();
        WebElement usernameField = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("userName")));
        usernameField.sendKeys(username);
        WebElement passwordField = driver.findElement(By.id("password"));
        passwordField.sendKeys(password);
        WebElement loginButton = driver.findElement(By.id("loginButton"));
        loginButton.click();
		// 获取所有 cookie
        Set<Cookie> cookies = driver.manage().getCookies();

        System.out.println("所有 cookie:");
        for (Cookie cookie : cookies) {
            System.out.println(String.format("名称: %s, 值: %s, 域: %s, 路径: %s, 过期: %s",
                    cookie.getName(), cookie.getValue(), cookie.getDomain(),
                    cookie.getPath(), cookie.getExpiry()));
            }
        // 获取特定的 cookie
        Cookie specificCookie = driver.manage().getCookieNamed("token");

        WebElement message = driver.findElement(By.id("message"));
        message.getText();

        driver.quit();
    }
}
相关推荐
安冬的码畜日常3 小时前
【JUnit实战3_20】第十一章:用 Gradle 运行 JUnit 测试实战
测试工具·junit·单元测试·gradle·软件构建·groovy·junit5
张永清-老清4 小时前
每周读书与学习->JMeter主要元件详细介绍(三)逻辑控制器
测试工具·jmeter·压力测试·性能调优·jmeter性能测试·性能分析·每周读书与学习
程序员三藏5 小时前
如何使用Selenium做自动化测试?
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
安冬的码畜日常7 小时前
【JUnit实战3_19】第十章:用 Maven 3 运行 JUnit 测试(下)
java·测试工具·junit·单元测试·maven·junit5
安冬的码畜日常8 小时前
【JUnit实战3_18】第十章:用 Maven 3 运行 JUnit 测试(上)
测试工具·junit·maven·artifact·junit5
交换机路由器测试之路9 小时前
发包工具anysend使用手册
网络·测试工具·ipv6·发包工具
jc062012 小时前
7.1-性能与测试工具
测试工具
程序员杰哥15 小时前
外包干了三年,快要废了。。。
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
安冬的码畜日常1 天前
【JUnit实战3_13】第八章:mock 对象模拟技术在细粒度测试中的应用(上)
测试工具·junit·单元测试·junit5·mock模拟·mock对象·mock objects
天才测试猿1 天前
Selenium定位元素的方法css和xpath的区别
css·自动化测试·软件测试·python·selenium·测试工具·测试用例