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

相关推荐
阿郎_20111 小时前
python自动化脚本-下载小说
python·selenium·网络爬虫
软件测试雪儿2 小时前
自动化测试面试真题(附答案)
软件测试·测试工具·面试·职场和发展
reddingtons2 小时前
Illustrator 3D Mockup:零建模,矢量包装一键“上架”实拍
人工智能·ui·3d·aigc·illustrator·设计师·平面设计
言之。3 小时前
AI时代的UI发展
人工智能·ui
软件测试雪儿3 小时前
2025年100道最新软件测试面试题,常见面试题及答案汇总
软件测试·测试工具·职场和发展
码上成长4 小时前
组件库提速:Storybook + Chromatic + Visual Test 实战
前端·自动化
霍格沃兹测试开发学社5 小时前
被裁后,我如何实现0到3份大厂Offer的逆袭?(内附面试真题)
人工智能·selenium·react.js·面试·职场和发展·单元测试·压力测试
5G云网络6 小时前
ComfyUI资源库
ui
AI绘画小336 小时前
CTF 逆向一脸懵?这篇攻略把破解技巧讲透,新手也能快速上手!
测试工具·web安全·网络安全
qq_526099136 小时前
实时工业图像采集卡 | 低延迟传输,满足自动化生产线需求
运维·自动化