JojoMiss

疯魔jojo的后花园

导航

java Selenium,定位 伪元素.UI自动化

Java中,要获取这个表单字段前面的星号“*”,因为是用的伪元素,无法直接通过常规定位获取字符,需要用到 JavascriptExecutor。

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);
}

执行结果:

 

posted on 2024-07-20 11:31  幽忧一世  阅读(9)  评论(0编辑  收藏  举报

⭐️CSDN 我的文章列表⭐️
⭐WEIBO 微博首页⭐️
⭐园子 我的院子⭐️