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

相关推荐
qq_267612892 小时前
Gitee DevOps的度量驱动持续改进:从构建数据到交付效率的量化管理与优化路径
前端·gitee·自动化
VIP_CQCRE2 小时前
用 Ace Data Cloud Studio,把内容营销变成自动运行的 AI 工作流
ai·自动化·内容营销·mcp·acedatacloud
中才实用2 小时前
Jenkins自动化部署
运维·自动化·jenkins
UnicornIT3 小时前
【HarmonyOS】时间管理类APP:做成“自适应“
ui·华为·harmonyos·鸿蒙
我的xiaodoujiao13 小时前
快速学习Python基础知识详细图文教程17--类型注解和断点调试
开发语言·python·学习·测试工具
阿Q论文16 小时前
机械设计制造及其自动化/ 机械电子工程 / 机电一体化技术(四自由度的工业机器人)
机器人·自动化·制造
泽众云测试20 小时前
泽众软件与日联科技达成合作,TestOne自动化测试平台助力搭建工业检测装备数字化品控防线
功能测试·测试工具·自动化
向上的车轮20 小时前
GitHub Actions 自动化运维实战:Rust 全栈项目 CI/CD 至华为云
运维·自动化·github
鹿鹿学长21 小时前
从英伟达开源Nemotron 3.5看“本地跑大模型“:一张显卡够用的时代来了
python·自动化