問題描述
我正在嘗試驗證提供的信息無效或不完整"的文字.在 Java 中使用 Selenium/WebDriver 顯示.
I am trying to verify the text "The information provided is either invalid or incomplete." is displayed using Selenium/WebDriver in Java.
我有以下 HTML:
<div id="validationError">
<ul>
<li>Required: Server Name</li>
<li>Required: Receiving Port</li>
</ul>
</div>
使用以下 CSS 代碼:
with the following CSS code:
#validationError:before {
content: 'The information provided is either invalid or incomplete.';
}
這是我當前的 java 代碼:
Here's my current java code:
WebElement errorBox = browser.findElement(By.id("validationError"));
Assert.assertNotNull("Could not find validation errors", errorBox);
System.out.println("Error Explanation: " + error.getCssValue("content") + "
");
List<WebElement> errorList = errorBox.findElements(By.tagName("li"));
for (WebElement error : errorList) {
System.out.println("Error: " + error.getText());
}
System.out.println("
Error Full Text: " + errorBox.getText());
這是我上面代碼的輸出:
This is my output of the above code:
Error Explanation:
Error: Required: Server Name
Error: Required: Receiving Port
Error Full Text: Required: Server Name
Required: Receiving Port
我似乎找不到驗證文本提供的信息無效或不完整"的方法.被陳列.在 web 元素上調用 getText()
也不會返回預期的字符串,但會在該 div 中顯示任何其他正常文本.有什么想法嗎?
I can't seem to find a way to verify the text "The information provided is either invalid or incomplete." is displayed. Calling getText()
on the web element also does not return the expected string, but will display any other normal text within that div. Any ideas?
推薦答案
我不能 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.
這篇關于Selenium WebDriver 從 CSS 屬性“內容"獲取文本在 ::before 偽元素上的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!