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();
    }
}
相关推荐
北岛三生13 小时前
ISP(图像信号处理器)
图像处理·数码相机·测试工具·计算机视觉·测试用例·模块测试
北岛三生1 天前
Camera tuning flow相机调试流程
图像处理·数码相机·测试工具·模块测试
晋人在秦 老K1 天前
入梦工具箱怎么检测硬件?3步完成CPU-Z跑分测试 硬件检测总出错?图吧工具箱免费功能实测 draw.io 部署指南:私有化流程图服务搭建教程
测试工具·流程图·工具·draw.io
zhangzeyuaaa2 天前
Selenium 超时完全指南:pageLoadTimeout、implicitlyWait 和 scriptTimeout 的深度解析
selenium·测试工具
泛联新安2 天前
如何根据项目需求选择合适的软件测试工具?iUnit智能单元测试平台提供专业化解决方案
c++·测试工具·单元测试
c萱3 天前
软件测试错题笔记
软件测试·数据库·笔记·测试工具·oracle·测试用例
测试开发Kevin4 天前
详解Grafana k6 的阈值(Thresholds)
测试工具·压力测试
kebeiovo4 天前
常用的几种测试工具:selenium,jmeter,jenkins
selenium·测试工具·jmeter
小白学大数据4 天前
应对反爬:使用Selenium模拟浏览器抓取12306动态旅游产品
selenium·测试工具·旅游