java Selenium,定位 伪元素.UI自动化

Java中,要获取这个表单字段前面的必填标识星号"*",因为是用的伪元素,无法直接通过常规定位获取字符,需要用到 JavascriptExecutor。

java 复制代码
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public static void queryInfo(WebDriver driver){
    // 目标元素的xpath路径
    String time="//*[@id=\"rc-tabs-0-panel-1\"]/div[2]/div/div/form/div[1]/div/div[1]/label";
    // 获取元素
    WebElement element =driver.findElement(By.xpath(time));
    // 获取伪元素
    JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
    String display =  jsExecutor.executeScript("return window.getComputedStyle(arguments[0], ':before').getPropertyValue('content')",element).toString();
    System.out.println(display);
}

执行结果:

感谢大佬的案例:无法使用Selenium中的javascript执行器定位伪元素-腾讯云开发者社区-腾讯云

相关推荐
goehou11 小时前
让 AI 替我操作这台电脑,先把手能伸多远定下来
windows·自动化·权限管理·ai agent
Frankie_stand12 小时前
测试自动化实战指南:从脚本化、自动判据到 CI 集成的完整落地路径
android·ci/cd·自动化
FungLeo13 小时前
成为全栈·Vue 管理后台篇·Vue3 + Vite + Naive UI:后台工程基座怎么搭
前端·vue.js·ui·typescript·vite·前端工程化·biome
freejackman1 天前
【工程化】从“单兵作战”到“多引擎架构”:基于门面模式的重构实战
大数据·数据库·hive·sql·测试工具·设计模式·架构
敲上瘾1 天前
Qt Widgets入门:项目模板解析与Hello World
开发语言·数据库·c++·qt·ui·qt6.3
坐吃山猪2 天前
Python22-PostMan调试MCP
测试工具·postman·mcp
总裁余(余登武)2 天前
【python项目】:1操作EXCEl自动化开发
python·自动化·excel
海洋之恒2 天前
GitHub Actions 自动化:5 个真实运维提效案例
运维·自动化·github
lisw052 天前
用于医疗的磁驱动柔性微机器人研究进展!
机器人·自动化·硬件工程
Sakura_j_h_2 天前
从零写一个 MCP stdio Server:用 Python 校验博客文章格式
开发语言·python·自动化·ai工具·mcp·json-rpc