我正在尝试验证文本“提供的信息无效或不完整。”在 Java 中使用 Selenium/WebDriver 显示。
我有以下 HTML:
<div id="validationError">
<ul>
<li>Required: Server Name</li>
<li>Required: Receiving Port</li>
</ul>
</div>
使用以下 CSS 代码:
#validationError:before {
content: 'The information provided is either invalid or incomplete.';
}
这是我当前的 java 代码:
WebElement errorBox = browser.findElement(By.id("validationError"));
Assert.assertNotNull("Could not find validation errors", errorBox);
System.out.println("Error Explanation: " + error.getCssValue("content") + "\n");
List<WebElement> errorList = errorBox.findElements(By.tagName("li"));
for (WebElement error : errorList) {
System.out.println("Error: " + error.getText());
}
System.out.println("\nError Full Text: " + errorBox.getText());
这是我上面代码的输出:
Error Explanation:
Error: Required: Server Name
Error: Required: Receiving Port
Error Full Text: Required: Server Name
Required: Receiving Port
我似乎找不到验证文本“提供的信息无效或不完整”的方法。被陈列。在 web 元素上调用 getText()
也不会返回预期的字符串,但会显示该 div 中的任何其他正常文本。有什么想法吗?
最佳答案
我不是 100% 确定 WebDriver 是否可以为您检索伪元素内容。我认为您需要使用 Javascript。下面的作品,我测试过。
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.
关于css - Selenium WebDriver 从::before 伪元素上的 CSS 属性 "content"获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28244911/