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

相关推荐
啦啦啦!1 小时前
基于AI进行GUI自动化测试
功能测试·测试工具·ai编程
jin1233222 小时前
HarmonyOS ArkTS API 24实现声明式 UI 框架与 @Builder 组件复用模式构建三标签页智能账单应用
ui·华为·harmonyos
Ai_easygo3 小时前
多Agent数据分析报告自动化实战:用CrewAI组支AI团队,丢份数据就出报告(从架构到评估)
人工智能·数据分析·自动化
我送炭你添花3 小时前
HART协议详解:00 为什么工业世界仍然需要HART?
网络·机器人·自动化·智能工厂
江边风声4 小时前
从薄板到厚板、从吸盘到夹板边——坤鹏伯爵的取放技术体系是怎样覆盖全制程的
人工智能·科技·自动化·制造·pcb工艺
Hacker_xingchen4 小时前
手把手教你配置Jenkins自动化邮件通知
自动化测试·软件测试·servlet·自动化·jenkins
迷茫中的自我5 小时前
GitHub Actions自动化运维实战:从CI/CD到云原生部署
运维·自动化·github
曦尧13 小时前
Microsoft Ontology Playground:用纯前端工具打通 Fabric IQ 本体论学习通路
ai·自动化
兆龙电子单片机设计15 小时前
【STM32项目开源】STM32单片机语音分类垃圾桶-蓝牙APP
stm32·单片机·嵌入式硬件·物联网·开源·自动化·毕业设计
会飞的小新17 小时前
Windows CMD 与 PowerShell 深度解析:底层原理、命令差异、自动化选型指南
运维·windows·自动化