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

相关推荐
紧固件研究社30 分钟前
冷镦设备高产、低耗与环保并行的制造路径
自动化·制造·紧固件
视觉光源老郑41 分钟前
从“看见“到“看清“,思奥特智能用技术重新定义工业视觉检测
自动化·机器视觉光源·视觉检测光源工厂
知凡D2 小时前
python脚本打包成exe后,对其引用的日历库实时更新-动态化加载模块
python·测试工具
Wpa.wk2 小时前
接口测试-Postman接口测试流程小练习2
测试工具·postman
风吹落叶花飘荡2 小时前
2026 在阿里云(Aliyun)上实现 Certbot 自动化申请
阿里云·自动化·云计算
阿蔹2 小时前
UI测试自动化-Web-Python-Selenium-2-元素操作、浏览器操作
前端·python·selenium·ui·自动化
专注VB编程开发20年2 小时前
VB6 UIAutomation 自动化查找元素
运维·自动化·uiautomation·uia
wenzhangli73 小时前
Ooder框架8步编码流程实战 - DSM组件UI统计模块深度解析
windows·ui
MoonBit月兔3 小时前
用 MoonBit 打造的 Luna UI:日本开发者 mizchi 的 Web Components 实践
前端·数据库·mysql·ui·缓存·wasm·moonbit