Selenium WebDriver 从 CSS 属性“内容"获取文本在 ::before 伪元素上

Selenium WebDriver get text from CSS property quot;contentquot; on a ::before pseudo element(Selenium WebDriver 从 CSS 属性“内容获取文本在 ::before 伪元素上)

本文介绍了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 伪元素上

基础教程推荐