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();
    }
}
相关推荐
知凡D12 小时前
python脚本打包成exe后,对其引用的日历库实时更新-动态化加载模块
python·测试工具
Wpa.wk12 小时前
接口测试-Postman接口测试流程小练习2
测试工具·postman
阿蔹12 小时前
UI测试自动化-Web-Python-Selenium-2-元素操作、浏览器操作
前端·python·selenium·ui·自动化
程序员杰哥15 小时前
Postman设置接口关联,实现参数化
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
oh-pinpin15 小时前
【jmeter】-脚本-导入添加接口(F12复制 cURL )
测试工具·jmeter·压力测试
软件测试小仙女17 小时前
告别手工分析!Python+HAR一键生成页面性能测试报告
自动化测试·软件测试·python·测试开发·测试工具·性能测试·测试报告
汽车仪器仪表相关领域17 小时前
亲历机动车排放检测升级:南华NHA-604/605测试仪的实战应用与经验沉淀
人工智能·功能测试·测试工具·安全·汽车·压力测试
BullSmall17 小时前
Anroid、IOS、鸿蒙、浏览器的兼容性测试方案
测试工具·可用性测试
fzm529818 小时前
嵌入式软件单元测试中AI自动化与人工检查的协同机制研究:基于专业工具的实证分析
c语言·测试工具·单元测试·自动化