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

相关推荐
Allen_LVyingbo23 分钟前
三甲医院AI的1163实施路径(上)
人工智能·自然语言处理·自动化·健康医疗·数据库架构
ck01357928 分钟前
视频添加水印的实践:文字叠加、透明 PNG 与时间区间设置
图像处理·测试工具·音视频·视频
Rock Chin6971 小时前
LangBot 群机器人:三个 AI 角色分工迎新人、答群规、管广告
人工智能·机器人·开源·自动化
艺杯羹2 小时前
Android无障碍自动化死循环全解:从动态角标伪匹配到单向状态机防御实战
人工智能·自动化·大语言模型
Maydaycxc3 小时前
gentic RPA 深度解析|AI Agent 与 RPA 混合架构、落地痛点、选型要点
人工智能·ai·自动化·rpa
且随疾风前行.18 小时前
Paint Cavans高级绘制:[一]Paint画笔高级应用
android·ui
唯鹿18 小时前
AI生成UI代码实践(二)—— Figba 更新月记
ui·ai·figma·1024程序员节·蓝湖
fqbqrr19 小时前
2610C++,UI复合技术在窗口中绘画字体图标
c++·ui
xiejava10181 天前
【一个人的SOC】(12) AI 安全报告与主动推送——五种场景的自动化闭环
人工智能·安全·自动化·开源软件·安全运营·ai soc