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

相关推荐
跨境小彭44 分钟前
店群运营实操复盘:批量活动申报自动化优化方案
大数据·运维·人工智能·自动化·跨境电商·temu·temu电商运营
Jlzn88881 小时前
2026年锂电CCS组装线:车规储能双场景兼容自动化方案的技术架构与实践
运维·架构·自动化
盛夏绽放1 小时前
Proxy与Reflect详解(二):进阶篇——手写Vue3响应式系统
ui·vue3·proxy
St_rive1 小时前
selenium元素定位方法
selenium·测试工具
St_rive1 小时前
selenium常用等待方式
selenium·测试工具
代码方舟2 小时前
企业级对公银行 KYC 架构:基于天远人脸身份证比对A构建自动化客户尽调网关
运维·人工智能·架构·自动化
造火箭2 小时前
Android UI自动化测试可行性评估SKILL
android·功能测试·ui
天空属于哈夫克32 小时前
企业微信怎么发图片和文件?文本接口发不了附件怎么办
机器人·自动化·企业微信
Bruce_Liuxiaowei3 小时前
自动化渗透测试——用AI构建自动化渗透测试流程
运维·人工智能·自动化
梦想的旅途23 小时前
企业微信SCRM怎么对接API?私域自动化常用接口清单
运维·自动化·企业微信