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

相关推荐
FII工业富联科技服务2 小时前
从大模型到自主运营:Intelligent UI如何成为工业AI的新入口
人工智能·ui
袁袁袁袁满2 小时前
AI Agent 如何“看懂“互联网?
爬虫·python·自动化·爬虫实战·多线程爬虫
zxcvb1533 小时前
规定备份信息的备份方式:从策略制定到自动化落地的实践思考
运维·oracle·自动化
eBest数字化转型方案5 小时前
经销商对账自动化:从五本账模型到票据识别的工程实现
运维·自动化
Elastic 中国社区官方博客5 小时前
AWS 自动化根因分析:从 CloudWatch 告警到完成故障诊断,仅需 36 秒
大数据·运维·elasticsearch·自动化·aws
xixiaoyunya6 小时前
高校教学资料备份:校园内网自动化备份完整方案
运维·自动化
浪子明X6 小时前
AI 日报自动化:先把事实、时间和引用做成可检查的编辑流水线
运维·人工智能·自动化
qq_252959977 小时前
ChatGPT突然长出“可操作界面”:GPT-6 Intelligent UI让答案变成计算器、地图和小游戏
gpt·ui·chatgpt
A-刘晨阳7 小时前
GitLab + ArgoCD 实现 Kubernetes GitOps 自动化部署
运维·人工智能·git·kubernetes·自动化·云计算·argocd