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

相关推荐
驭白.4 小时前
不止于自动化:新能源汽车智造的数字基座如何搭建?
大数据·人工智能·自动化·汽车·数字化转型·制造业
爆更小哇4 小时前
Selenium自动化测试函数全解析(二)
java·selenium·测试工具·自动化
我想吃烤肉肉8 小时前
Python 中 asyncio 是什么?
爬虫·python·自动化
云飞云共享云桌面8 小时前
非标自动化工厂的设计云桌面为什么要选云飞云智能共享云桌面?
大数据·运维·服务器·网络·自动化·负载均衡
leiming610 小时前
c++ QT 开发第二天,用ui按钮点亮实体led
开发语言·qt·ui
努力改掉拖延症的小白10 小时前
2026年大模型、RAG、智能体、Palantir本体论落地实现业务价值的核心底层逻辑总结
人工智能·语言模型·自动化
0思必得011 小时前
[Web自动化] BeautifulSoup导航文档树
前端·python·自动化·html·beautifulsoup
程序员小远12 小时前
UI自动化测试框架:PO模式+数据驱动
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
FreeBuf_12 小时前
新型TCC绕过漏洞:macOS面临自动化攻击风险
macos·自动化·策略模式
@杨某13 小时前
超级鹰的使用
爬虫·selenium