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

相关推荐
Land03291 小时前
RPA工具选型技术指南:架构差异与实测数据
python·自动化·rpa
志栋智能3 小时前
告别报告堆砌:超自动化巡检的智能分析与洞察
运维·服务器·网络·人工智能·自动化
测试_AI_一辰3 小时前
AI 产品输出格式测试实战:为什么模型返回的 JSON 前端解析总报错
人工智能·ai·自动化·状态模式·ai编程
雅斯驰4 小时前
AES-128加密+滚动码认证:ATA5702W如何防御中继攻击与信号重放
运维·单片机·嵌入式硬件·物联网·自动化
IT空门:门主5 小时前
Pixso UI + Figma + ui-ux-pro-max +ai idae工作流教程
ui·ux·figma·ai idae
qq_452396236 小时前
第十八篇:《移动端UI自动化:Appium入门实战》
ui·appium·自动化
罗西的思考7 小时前
【GUI-Agent】阿里通义MAI-UI 代码阅读(1)— 总体
人工智能·机器学习·ui·transformer
码途漫谈8 小时前
UI-UX-Pro-Max开源项目介绍
人工智能·ui·ai·开源·ai编程·ux
比特 GOK8 小时前
Qt项目ui文件中新添加的控件在代码中不识别的问题解决
开发语言·qt·ui
TTGGGFF11 小时前
自动化天塌了?AI 替你画 Simulink!Agent——MCP 配置踩坑指南(附真实环境实测)
人工智能·自动化·simulink