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

相关推荐
宇钶宇夕13 小时前
ATV900系列变频器起重提升抱闸逻辑设置及源型接线指南
运维·自动化
月明长歌13 小时前
Selenium中隐式等待(Implicit Wait)和显式等待(Explicit Wait)的区别
前端·javascript·selenium
云飞云共享云桌面13 小时前
上海模具制造工厂10人用一台共享电脑做SolidWorks设计
linux·运维·服务器·网络·自动化
CodeCraft Studio14 小时前
从框架到体验:Qt + Qtitan 构建制造业嵌入式UI整体解决方案
开发语言·qt·ui·gui·嵌入式开发·hmi·制造业嵌入式ui
AI_零食14 小时前
鸿蒙的flutter框架表达:生命律动系统
学习·flutter·ui·华为·harmonyos·鸿蒙
AI_零食14 小时前
鸿蒙跨端框架 Flutter 学习 Day 6:Future 在 UI 渲染中的心跳逻辑
学习·flutter·ui·华为·harmonyos·鸿蒙
缺点内向14 小时前
C# 高效统计 Word 文档字数:告别手动,拥抱自动化
c#·自动化·word
技术与健康14 小时前
【 Markdown Illustrator - 自动配图系统】
ui·illustrator
b***251114 小时前
激光焊接机如何破解新能源电池制造的三大焊接难题?
人工智能·自动化·制造
Rhys..14 小时前
python + selenium 如何定位动态元素
开发语言·python·selenium