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

相关推荐
企微自动化14 分钟前
如何有效规避企业微信的自动化风控检测
运维·自动化·企业微信
小嘟嘟1316 分钟前
Kurator深度解析:云原生多集群管理的高效解决方案
linux·运维·docker·云原生·自动化
2501_939909051 小时前
自动化运维工具 Ansible 集中化管理服务器
运维·自动化·ansible
hanyi_qwe1 小时前
自动化运维工具 Ansible 集中化管理服务器
运维·自动化·ansible
liliangcsdn1 小时前
如何在mac m1模拟鼠标与键盘的自动化操作
自动化·计算机外设
2501_9240641114 小时前
2025年接口错误自动分析工具对比与性能测试平台选型指南
测试工具·性能优化·数据可视化
weixin_3077791314 小时前
Jenkins Pipeline 完全指南:核心概念、使用详解与最佳实践
开发语言·ci/cd·自动化·jenkins·etl
小新11015 小时前
vs2022+Qt插件初体验,创建带 UI 界面的 Qt 项目
开发语言·qt·ui
tzhou6445217 小时前
自动化运维利器Ansible
运维·自动化·ansible
AALoveTouch17 小时前
n8n 2.0 中文汉化版一键部署教程 | 解除Execute Command限制
人工智能·自动化