我正在尝试验证文本“提供的信息无效或不完整”.在
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.