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);
}
执行结果:
标签:java,JavascriptExecutor,selenium,Selenium,openqa,UI,org,import,div From: https://www.cnblogs.com/JojoMiss/p/18312902