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

相关推荐
weixin_402486341 天前
在adobe illustrator 上写latex code 显示数学公式 安装 LaTeX2Illustrator
ui·adobe·illustrator
写代码的【黑咖啡】1 天前
Python中的Selenium:强大的浏览器自动化工具
python·selenium·自动化
~黄夫人~1 天前
Ansible 自动化运维:从 “手动输密码” 到 “一键免密管理”
linux·运维·自动化·ansible
可可南木1 天前
3070文件格式--6--board文件格式详解 6
功能测试·测试工具·pcb工艺
王九思1 天前
Ansible 自动化运维介绍
运维·自动化·ansible
三不原则1 天前
实战:基于 GitOps 实现 AI 应用的自动化部署与发布
运维·人工智能·自动化
AALoveTouch1 天前
分享演唱会攻略-抢票利器
小程序·自动化
程序员雷叔1 天前
在postman设置请求里带动态token,看看这两种方法!
selenium·测试工具·单元测试·测试用例·pytest·lua·postman
Hacker_xingchen1 天前
如何用Postman做接口自动化测试及完美的可视化报告?
自动化测试·软件测试·测试工具·职场和发展·postman
AI软件工程实践1 天前
软件工程里 Postman 的文件上传与下载测试技巧
测试工具·ai·lua·postman