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

相关推荐
科立分板机源头厂家43 分钟前
第29集科立分板机:降本增效新选择科立自动化分板机赋能电子企业高质量发展
自动化·分板机
我的xiaodoujiao1 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 25--数据驱动--参数化处理 Excel 文件 2
前端·python·学习·测试工具·ui·pytest
汪汪队立大功1232 小时前
selenium中执行javascript,是否等价于在浏览器console位置执行
javascript·selenium·测试工具
kewu89712 小时前
Postman 完全入门教程
测试工具·lua·postman
TG:@yunlaoda360 云老大5 小时前
谷歌云发布 Document AI Workbench 最新功能:自定义文档拆分器实现复杂文档处理自动化
运维·人工智能·自动化·googlecloud
守城小轩6 小时前
基于Chrome140的X账号自动化——需求分析&环境搭建(一)
自动化·rpa·浏览器自动化·playwright
top_designer6 小时前
Firefly 样式参考:AI 驱动的 UI 资产“无限”生成
前端·人工智能·ui·aigc·ux·设计师
北京耐用通信7 小时前
不只是延长,是“重生”:耐达讯自动化Profibus总线光端机如何让老旧设备数据“开口说话”?
人工智能·物联网·网络协议·自动化·信息与通信
卖个几把萌8 小时前
【06】JMeter获取接口返回变量,传给下一个接口(正则表达式提取、json提取)
测试工具·jmeter