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

相关推荐
卓码软件测评6 小时前
第三方软件测试测评机构【使用web_reg_save_param_ex函数:掌握LoadRunner关联的黄金法则 】
测试工具·ci/cd·性能优化·单元测试·测试用例
北京耐用通信8 小时前
耐达讯自动化Profibus总线光纤中继器在轨道交通信号系统中的应用
网络·科技·物联网·自动化·信息与通信
大飞记Python10 小时前
代码级教程|用Playwright实现Web自动化测试:从零到录制生成脚本
自动化测试·python·selenium·playwright
weixin_4387321011 小时前
ChromeDriver谷歌驱动下载
linux·chrome·selenium·自动化·mac·chrome devtools·chromedriver
快乐小胡!12 小时前
【自动化测试】Selenium选择/定位元素的基本方法
python·selenium·测试工具
守城小轩12 小时前
基于Chrome140的Youtube账号自动化——脚本撰写(二)
自动化·浏览器自动化·指纹浏览器·浏览器开发
Wpa.wk13 小时前
持续集成 - 持续集成工具-Jenkins的部署流程
java·运维·经验分享·ci/cd·自动化·jenkins
PhotonixBay14 小时前
车载HUD的 TFT 屏被动散热仿真与太阳光测试
测试工具
我送炭你添花14 小时前
Pelco KBD300A 模拟器:10.报警联动规则编辑与执行
python·自动化·运维开发
muddjsv15 小时前
从用户需求到产品体验:UI/UX 设计核心方法论与实战指南
ui