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

相关推荐
骆驼爱记录7 分钟前
3步删除Word目录空白行
自动化·word·excel·wps·新人首发
缺点内向9 分钟前
.NET办公自动化:Spire.Doc操作Word——文本框移除完整教程
c#·自动化·word·.net
袁煦丞 cpolar内网穿透实验室10 分钟前
ansible批量自动化清理服务器日志+远程管理!cpolar 内网穿透实验室第 710 个成功挑战
服务器·自动化·ansible·远程工作·内网穿透·cpolar
少云清1 小时前
【UI自动化测试】6_TPshop项目实战 _allure测试报告
ui
少云清3 小时前
【UI自动化测试】3_TPshop项目实战 _一个测试用例脚本编写过程
ui·测试用例
Jasmine8393 小时前
OpenClaw部署太难? Codex全流程零编码实现浏览器UI自动化
人工智能·测试工具·ui
挨踢学霸8 小时前
技术全面重构|MsgHelper 新版深度拆解:交互、视觉与逻辑的底层优化(二)
经验分享·笔记·微信·架构·自动化
缺点内向8 小时前
C#实战:使用Spire.Doc for .NET 获取并替换Word文档中的字体
c#·自动化·word·.net
Will_11308 小时前
Linux运维自动化常用的Python库
linux·运维·自动化
不如摸鱼去8 小时前
uni-app 组件库 Wot UI 的 AI 友好型编程指南
人工智能·ui·uni-app