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

相关推荐
xy34532 小时前
wireshark 如何捕获信息
网络·测试工具·渗透测试·wireshark
其实防守也摸鱼3 小时前
运维--学习阶段问题解答(1)(自测)
linux·运维·服务器·数据库·学习·自动化·命令模式
GJGCY3 小时前
财务智能体落地实践:RPA、大模型与规则引擎如何构建业财自动化闭环?
经验分享·ai·自动化·财务·智能体
CedarQR8 小时前
VS Code Copilot Chat 配置 MCP Server 全攻略(以 Next AI Draw.io 为例)
人工智能·ui·json
会周易的程序员9 小时前
从零构建多核CPU负载自适应控制系统
linux·c++·笔记·物联网·测试工具
博观而约取厚积而薄发9 小时前
Pytest 从入门到精通,一篇就够(超详细实战教程)
python·测试工具·单元测试·自动化·pytest
imzed9 小时前
使用 Playwright + Pytest 构建 Web UI 自动化测试框架
python·自动化·pytest
交友如交10 小时前
对于Prompt的思考:从“手写”到提示词采样、A/B Test 与自动化评测
人工智能·自动化·prompt
Amy1870211182310 小时前
告别“爬楼抄表”时代:电梯计量自动化改造,让每一度电都“开口说话”
运维·自动化