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

相关推荐
12点一刻6 小时前
搭建自动化工作流:探寻解放双手的有效方案(2)
运维·人工智能·自动化·deepseek
未来之窗软件服务6 小时前
东方仙盟AI数据中间件使用教程:开启数据交互与自动化应用新时代——仙盟创梦IDE
运维·人工智能·自动化·仙盟创梦ide·东方仙盟·阿雪技术观
ajassi20007 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
UI罐头7 小时前
后台设计指南:系统架构、交互规范与工具实战全流程解析
ui·设计·设计工具
宇钶宇夕9 小时前
SIMATIC S7-1200的以太网通信能力:协议与资源详细解析
运维·服务器·数据库·程序人生·自动化
UI前端开发工作室10 小时前
UI前端与数字孪生融合新领域拓展:智慧教育的虚拟实验室建设
ui
amazinging11 小时前
北京-4年功能测试2年空窗-报培训班学测开-第四十七天
python·学习·selenium
耀耀_很无聊11 小时前
[特殊字符] 05_Jenkins 部署前端项目实现自动化部署
前端·自动化·jenkins
Leinwin11 小时前
微软上线 Deep Research 预览版:o3+必应赋能研究自动化
运维·microsoft·自动化
Tomorrow'sThinker13 小时前
✍️ Python 批量设置 Word 文档多级字体样式(标题/正文/名称/小节)
python·自动化·word·excel