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();
    }
}
相关推荐
懒羊羊大王&1 小时前
软件测试之博客系统项目实战(补充和解析部分)
selenium·单元测试·测试用例·集成测试
0思必得02 小时前
[Web自动化] Selenium处理文件上传和下载
前端·爬虫·python·selenium·自动化·web自动化
测试人社区—03926 小时前
UI测试在DevOps流水线中的卡点设计:质量保障的智能防线
运维·驱动开发·测试工具·ui·ar·vr·devops
实时数据8 小时前
Selenium常用于网页爬取 为了提高爬取效率,可以采取以下优化措施:合理使用无头模式
selenium·测试工具·数据挖掘
zhaoyin19948 小时前
fiddler抓包工具使用
前端·测试工具·fiddler
实时数据9 小时前
网络爬虫已成为获取互联网数据的重要手段。Selenium 作为一种强大的自动化测试工具,
爬虫·selenium·测试工具
佟以冬10 小时前
Wireshark抓包基础
网络·测试工具·安全·网络安全·wireshark
实时数据10 小时前
优化 Selenium 使用文本挖掘在分析留言数据中提供了多种应用 如情感分析、主题建模、关键词提取和文本分类
selenium·测试工具
灵翼飞航科技有限公司10 小时前
无人机动力测试系统光电传感器原理及操作说明(二)
测试工具·无人机
灵翼飞航科技有限公司11 小时前
要完成无人机动力测试需要都需要准备什么?
测试工具·无人机