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

相关推荐
St_rive42 分钟前
webUI自动化实现及封装
运维·自动化
qq_513728041 小时前
浏览器配置 + 文件上传 + 截图
python·selenium·测试工具·计算机外设
QYR_111 小时前
2032年气相分解设备市场规模达2.25亿美元:VPD向高灵敏度、自动化晶圆污染检测升级
运维·自动化
Είναι η κοπέλα2 小时前
游戏窗口自动化点击与防误点设计:模板匹配 + 区域限制 + 强制置顶
运维·游戏·自动化
深念Y2 小时前
AI Agent 时代运维安全:rm 防误删方案对比
linux·运维·人工智能·安全·自动化·agent
TELL5213 小时前
启动后端服务跑第一个用例成功,重新跑第二个用例chromedriver死活不成功,猜测是chromedriver本身的问题
selenium·webdriver
环境栈笔记5 小时前
指纹浏览器怎么用:从 Profile、代理到环境检测的完整上手流程
前端·人工智能·后端·自动化
PhotonixBay6 小时前
从粗糙度到三维形貌:激光共聚焦显微镜实现微米级表面分析
图像处理·人工智能·测试工具·算法
qq_513728046 小时前
去除自动化特征
自动化
山川志~6 小时前
Vue + Element UI 实战:如何实现表格时间、金额字段排序
vue.js·ui·elementui