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

相关推荐
洛克希德马丁3 小时前
QLineEdit增加点击回显功能
c++·qt·ui
struggle20255 小时前
ebook2audiobook开源程序使用动态 AI 模型和语音克隆将电子书转换为带有章节和元数据的有声读物。支持 1,107+ 种语言
人工智能·开源·自动化
漫谈网络7 小时前
Telnet 类图解析
python·自动化·netdevops·telnetlib·网络自动化运维
孙克旭_13 小时前
PXE_Kickstart_无人值守自动化安装系统
linux·运维·自动化
2301_7875528714 小时前
console-chat-gpt开源程序是用于 AI Chat API 的 Python CLI
人工智能·python·gpt·开源·自动化
程序员小远16 小时前
自动化测试与功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
CodeCraft Studio17 小时前
数据透视表控件DHTMLX Pivot v2.1发布,新增HTML 模板、增强样式等多个功能
前端·javascript·ui·甘特图
xbd_zc1 天前
【Jenkins简单自动化部署案例:基于Docker和Harbor的自动化部署流程记录】
docker·自动化·jenkins·harbor
第三方软件测评1 天前
第三方软件测评中心分享:软件功能测试类型和测试工具
功能测试·测试工具
Anesthesia丶1 天前
Vue3 + naive-ui + fastapi使用心得
vue.js·ui·fastapi