Selenium WebDriver从CSS属性“content”获取文本on :: before伪元素 [英] Selenium WebDriver get text from CSS property "content" on a ::before pseudo element

查看:7006
本文介绍了Selenium WebDriver从CSS属性“content”获取文本on :: before伪元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证文字提供的信息无效或不完整。使用Selenium / WebDriver在Java中显示。

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代码:

#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") + "\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());

这是我上面代码的输出:

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属性“content”获取文本on :: before伪元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆