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 伪元素上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试验证提供的信息无效或不完整"的文字.在 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 伪元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

AttributeError: #39;list#39; object has no attribute #39;click#39; using Selenium and Python(AttributeError: list 对象没有使用 Selenium 和 Python 的属性 click)
How to execute JavaScript in Ruby written Webdriver test?(如何在 Ruby 编写的 Webdriver 测试中执行 JavaScript?)
ElementClickInterceptedException: Message: element click intercepted Element is not clickable error clicking a radio button using Selenium and Python(ElementClickInterceptedException:消息:元素单击被拦截元素不可点击错误单击使用 Selenium 和 P
An error has occured in #39;site url#39;: Uncaught TypeError: Cannot read property #39;getColomnSet#39; of undefined with Selenium and Python(“站点 url中发生错误:未捕获的类型错误:无法使用 Selenium 和 Python 读取未定义的属性“getColomnSet)
GIve css3 rotate to a DIV in Chrome then the background-attachment:fixed creating bug(将 css3 旋转到 Chrome 中的 DIV 然后背景附件:修复创建错误)
Changing width/height moves rotated element(改变宽度/高度移动旋转的元素)