且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Selenium WebDriver 从 CSS 属性“内容"获取文本在 ::before 伪元素上

更新时间:2023-02-19 17:33:16

我不能 100% 确定 WebDriver 是否可以为您检索伪元素内容.我认为您需要使用Javascript.下面的作品,我测试了.

I am not 100% sure if WebDriver can retrieve pseudo element content for you. I think you would need to use Javascript. Below works, I tested.

String script = "return window.getComputedStyle(document.querySelector('#validationError'),':before').getPropertyValue('content')";
JavascriptExecutor js = (JavascriptExecutor)driver;
String content = (String) js.executeScript(script);
System.out.println(content);

这应该打印出来

The information provided is either invalid or incomplete.