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

相关推荐
renhongxia17 小时前
如何对海洋系统进行知识图谱构建?
人工智能·学习·语言模型·自然语言处理·自动化·知识图谱
sheepfagdng10 小时前
python-web自动化-selenium(1)
selenium·测试工具
taxunjishu12 小时前
塔讯总线协议转换信捷 PLC 对接 TCP/IP 设备实战方案
网络·物联网·自动化
taxunjishu15 小时前
半导体晶圆制造车间 SITRANS P 仪表与 V90 伺服系统精密控制应用
网络·物联网·自动化
小周学学学15 小时前
vmware的python自动化:批量给esxi主机挂载iscsi动态目标
运维·自动化·vmware·虚拟化
牛奶咖啡1316 小时前
企鹅龙+再生龙服务器版实现自动化备份与还原系统实践
运维·自动化·企鹅龙·再生龙服务器版·系统批量自动化备份·系统批量自动化还原
Looooking16 小时前
Python 流程自动化之 DrissionPage 使用示例
python·自动化·drissionpage
SBFE16 小时前
使用minimax自动化本地部署openclaw操作历程
自动化
arvin_xiaoting17 小时前
2026年AI爆火新趋势:Agent协作与通信机制深度解析
人工智能·ai·自动化·agent·多智能体·通信机制·协作系统