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

相关推荐
星辰徐哥15 小时前
Spring Boot 数据导入导出与报表生成
spring boot·后端·ui
linyanRPA18 小时前
影刀RPA店群自动化实战:多店铺活动自动报名与促销管理架构设计
运维·自动化·办公自动化·rpa·python脚本·爬虫自动化·店群自动化
小鹿研究点东西18 小时前
直播带货长视频AI自动剪辑开播:一场直播如何反复利用?
ffmpeg·自动化·音视频·语音识别
for_ever_love__20 小时前
UI学习:UICollectionView瀑布流
学习·ui·ios·objective-c·cocoa
ting94520001 天前
SellerClaw 全栈技术深度拆解:基于多智能体集群的跨境电商全链路自动化系统实现
运维·自动化
xiaobai1781 天前
pytest+playwright实现UI自动化(4)-上夹具fixture
ui·自动化·pytest·playwright
可可南木1 天前
3070文件格式--21--fixture文件 3
功能测试·测试工具
为何创造硅基生物1 天前
LVGL 妙用 LV_OBJ_FLAG_FLOATING
ui
天空属于哈夫克31 天前
企微 RPA 接口开放:无需官方权限,外部群自由操作
自动化·企业微信·api
一只小白0001 天前
【JVM | 第五篇】—— 深入理解垃圾回收
jvm·测试工具