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

相关推荐
St_rive4 小时前
移动端自动化环境搭建与准备
运维·自动化
INNOVIX稳石机器人5 小时前
稳石500强实战验证:从人工仓储迈向AI自主智造,给出仓储自动化升级标准答案
运维·人工智能·自动化
JXJD20045 小时前
【无标题】
人工智能·ai·自动化
聪明蛋子哟5 小时前
浏览器自动化的“持久身份”:多会话认证与Agent技能的无缝集成实践
运维·自动化
代码方舟6 小时前
零信任架构实战:基于天远手机携号转网V即时版构建自动化营销风控网关
人工智能·智能手机·架构·自动化
志栋智能6 小时前
大模型驱动运维:重构故障根因分析的“思考”逻辑
运维·服务器·重构·自动化
OmniGoAI6 小时前
长任务为什么要分轮续跑
自动化·ai助理·gowork·任务续跑
JZMSYYQ7 小时前
电输运性质测试系统主要测试哪些方面
测试工具·材料测试·电性能·电性能测试·电输运测试·测试设备
greasyfork7 小时前
Mac Sketch v2026.3 矢量图设计工具
macos·ui·sketch