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执行器定位伪元素-腾讯云开发者社区-腾讯云

相关推荐
互联网科技看点6 小时前
深度解码狼人杀:2026年逻辑大师为何集体经典版?
自动化
悟空爬虫-彪哥8 小时前
2026 Python UI 框架选择指南:从 Streamlit 到 Pyside6 的四层体系
开发语言·python·ui
RReality9 小时前
【Unity UGUI】Toggle / ToggleGroup 与 Dropdown
ui·unity·游戏引擎·图形渲染·材质
ai_coder_ai9 小时前
自动化脚本ui编程之线性布局(linear)
ui·autojs·自动化脚本·冰狐智能辅助·easyclick
程序猿阿伟9 小时前
《QClaw隐藏的GitHub自动化神级用法》
运维·自动化·github
HYNuyoah10 小时前
3X-UI Reality 搭建指南
ubuntu·ui·docker
key_3_feng11 小时前
基于OpenClaw的Alibaba Cloud Linux 3自动化部署YashanDB深度方案
linux·运维·自动化·yashandb
GISer_Jing12 小时前
AI Agent Skills 发现指南:前端工程化与自动化全景
前端·人工智能·自动化
薛定猫AI13 小时前
【深度解析】零代码到 CLI 双路径构建 AI Agent:RAG、工具调用与自动化工作流实战
大数据·人工智能·自动化
workflower1 天前
人机交互部分OOD
运维·人工智能·自动化·集成测试·人机交互·软件需求