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

相关推荐
卓码软件测评5 小时前
第三方软件CMA/CNAS测评机构:【Apifox的自定义加密和签名的安全测试技巧】
测试工具·ci/cd·单元测试·测试用例·压力测试
0思必得06 小时前
[Web自动化] HTML5常见新增标签
前端·python·自动化·html5·web自动化
YJlio6 小时前
桌面工具学习笔记(11.4):BgInfo + Desktops + ZoomIt 组合拳——演示与排障环境一键到位
笔记·学习·自动化
测试人社区-小明6 小时前
未来测试岗位的AI需求分析
人工智能·opencv·测试工具·算法·金融·机器人·需求分析
慧都小项7 小时前
Parasoft Jtest集成Gradle教程:提速静态分析流程
java·测试工具
weixin_307779137 小时前
Jenkins Pipeline: Multibranch 插件详解:现代CI/CD的多分支管理利器
运维·开发语言·自动化·jenkins·etl
李星星BruceL7 小时前
Pytest第三章(参考指南1)
python·自动化·pytest
Low--Key7 小时前
pytest框架快速入门
python·自动化·pytest
古城小栈7 小时前
Spring Boot + 代理 AI:解锁供应链自动化决策新范式
人工智能·spring boot·自动化
weixin_307779138 小时前
Jenkins Folders插件详解:组织、管理与最佳实践
运维·开发语言·自动化·jenkins