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

相关推荐
GISer_Jing1 小时前
AI自动化工作流:智能驱动未来(升级研究生项目!!!)
人工智能·前端框架·自动化
志栋智能7 小时前
超自动化巡检:应对复杂IT环境的必然选择
运维·网络·安全·web安全·自动化
Mr -老鬼10 小时前
EasyClick 热更新坑点处理方案
自动化·ec·easyclick·易点云测
小陈的进阶之路10 小时前
Selenium元素定位
python·selenium
Agent产品评测局11 小时前
中小企业数字化转型,优先选 RPA 还是 AI Agent?:2026企业自动化架构选型深研
人工智能·ai·chatgpt·自动化·rpa
测试者家园11 小时前
从需求文档到测试点:利用大模型实现需求理解的自动化
软件测试·自动化·llm·需求分析·持续测试·智能化测试·功能点
GISer_Jing11 小时前
阿里开源纯前端浏览器自动化 PageAgent,[特殊字符] 浏览器自动化变天啦?
前端·人工智能·自动化·aigc·交互
I'm Jie12 小时前
Swagger UI 本地化部署,解决 FastAPI Swagger UI 依赖外部 CDN 加载失败问题
python·ui·fastapi·swagger·swagger ui
爱学习的程序媛13 小时前
【Web前端】优化Core Web Vitals提升用户体验
前端·ui·web·ux·用户体验
半个俗人13 小时前
06postman批量执行及参数化
测试工具·postman