Selenium WebDriver get text from CSS property quot;contentquot; on a ::before pseudo element(Selenium WebDriver 从 CSS 属性“内容获取文本在 ::before 伪元素上)
问题描述
我正在尝试验证提供的信息无效或不完整"的文字.在 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 伪元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Selenium WebDriver 从 CSS 属性“内容"获取文本在 ::before 伪元素上


基础教程推荐
- 检查 HTML5 拖放文件类型 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bokeh Div文本对齐 2022-01-01