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();
    }
}
相关推荐
黄卷青灯774 小时前
抓包工具 wireshark
网络·测试工具·wireshark
代码的乐趣5 小时前
支持selenium的chrome driver更新到133.0.6943.126
chrome·python·selenium
小白学大数据5 小时前
Selenium库详解:Python实现模拟登录与反爬限制的进阶指南
python·selenium·测试工具
看,未来17 小时前
Apipost 与 Postman 工具实践指南:WebSocket调试与动态参数测试
websocket·测试工具·postman
shimly12345617 小时前
tcpdump 用法示例
网络·测试工具·tcpdump
程序员老舅18 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
绿色果酱21 小时前
利用Postman和Apipost进行WebSocket调试和文档设计
websocket·测试工具·yapi·postman
测试19981 天前
Jmeter HTTP代理服务器录制压力脚本
自动化测试·软件测试·测试工具·jmeter·程序人生·职场和发展·测试用例
bug管理者1 天前
分享几款比较常用的接口测试工具
测试工具
程序员 小濠1 天前
接口测试基础 --- 什么是接口测试及其测试流程?
自动化测试·python·测试工具·职场和发展·appium·接口测试·压力测试