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

相关推荐
天空属于哈夫克31 小时前
企微 RPA 接口开放:无需官方权限,外部群自由操作
自动化·企业微信·api
一只小白0001 小时前
【JVM | 第五篇】—— 深入理解垃圾回收
jvm·测试工具
ANnianStriver2 小时前
PetLumina 04 — 管理后台 UI 全面升级
java·ui·ai编程
施努卡机器视觉2 小时前
电子水泵自动化生产线如何选型?SNK施努卡一站式集成方案解析
自动化
IT阿瑞3 小时前
制造业 AI Agent 实施服务商横评:2026 年企业级自动化选型全景分析
大数据·人工智能·自动化
2501_941982053 小时前
基于自动化控制架构的企业微信群消息管理系统设计
架构·自动化·企业微信
ANnianStriver3 小时前
PetLumina 05 — App 端 UI 效果应用
java·ui·ai编程
弹简特3 小时前
【接口自动化】02-Pytest固件fixture核心机制与Allure企业级报告实战
自动化·pytest·测试
小小龙学IT3 小时前
Midscene.js:AI驱动的跨平台UI自动化革命
javascript·人工智能·ui
tedcloud1233 小时前
Understand-Anything部署教程:打造AI代码理解平台
服务器·人工智能·学习·自动化·powerpoint