我正在尝试验证文本"提供的信息无效或不完整".在Java中使用Selenium/WebDriver显示.
我有以下HTML:
<div id="validationError">
<ul>
<li>Required: Server Name</li>
<li>Required: Receiving Port</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
使用以下CSS代码:
#validationError:before {
content: 'The information provided is either invalid or incomplete.';
}
Run Code Online (Sandbox Code Playgroud)
这是我目前的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());
Run Code Online (Sandbox Code Playgroud)
这是我输出的上述代码:
Error Explanation:
Error: Required: Server Name
Error: Required: Receiving Port
Error Full Text: Required: Server Name
Required: Receiving Port
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到验证文本的方法"提供的信息无效或不完整." 被展示.调用getText()web元素也不会返回预期的字符串,但会在该div中显示任何其他普通文本.有任何想法吗?
nil*_*esh 27
我不是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);
Run Code Online (Sandbox Code Playgroud)
这应该打印
The information provided is either invalid or incomplete.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15688 次 |
| 最近记录: |