1. <i id='h3Fx8'><tr id='h3Fx8'><dt id='h3Fx8'><q id='h3Fx8'><span id='h3Fx8'><b id='h3Fx8'><form id='h3Fx8'><ins id='h3Fx8'></ins><ul id='h3Fx8'></ul><sub id='h3Fx8'></sub></form><legend id='h3Fx8'></legend><bdo id='h3Fx8'><pre id='h3Fx8'><center id='h3Fx8'></center></pre></bdo></b><th id='h3Fx8'></th></span></q></dt></tr></i><div id='h3Fx8'><tfoot id='h3Fx8'></tfoot><dl id='h3Fx8'><fieldset id='h3Fx8'></fieldset></dl></div>

      <small id='h3Fx8'></small><noframes id='h3Fx8'>

      <tfoot id='h3Fx8'></tfoot>
    1. <legend id='h3Fx8'><style id='h3Fx8'><dir id='h3Fx8'><q id='h3Fx8'></q></dir></style></legend>

      • <bdo id='h3Fx8'></bdo><ul id='h3Fx8'></ul>

    2. Selenium WebDriver 从 CSS 属性“内容"获取文本在

      时间:2023-10-14

        <bdo id='sTHXi'></bdo><ul id='sTHXi'></ul>

        <small id='sTHXi'></small><noframes id='sTHXi'>

        <legend id='sTHXi'><style id='sTHXi'><dir id='sTHXi'><q id='sTHXi'></q></dir></style></legend>
          1. <tfoot id='sTHXi'></tfoot>

                <tbody id='sTHXi'></tbody>
            • <i id='sTHXi'><tr id='sTHXi'><dt id='sTHXi'><q id='sTHXi'><span id='sTHXi'><b id='sTHXi'><form id='sTHXi'><ins id='sTHXi'></ins><ul id='sTHXi'></ul><sub id='sTHXi'></sub></form><legend id='sTHXi'></legend><bdo id='sTHXi'><pre id='sTHXi'><center id='sTHXi'></center></pre></bdo></b><th id='sTHXi'></th></span></q></dt></tr></i><div id='sTHXi'><tfoot id='sTHXi'></tfoot><dl id='sTHXi'><fieldset id='sTHXi'></fieldset></dl></div>
                本文介绍了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 伪元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:ElementClickInterceptedException:消息:元素单击被拦截元 下一篇:如何在 Ruby 编写的 Webdriver 测试中执行 JavaScript

                相关文章

                最新文章

              • <tfoot id='Gs33T'></tfoot>
                  <bdo id='Gs33T'></bdo><ul id='Gs33T'></ul>
                <i id='Gs33T'><tr id='Gs33T'><dt id='Gs33T'><q id='Gs33T'><span id='Gs33T'><b id='Gs33T'><form id='Gs33T'><ins id='Gs33T'></ins><ul id='Gs33T'></ul><sub id='Gs33T'></sub></form><legend id='Gs33T'></legend><bdo id='Gs33T'><pre id='Gs33T'><center id='Gs33T'></center></pre></bdo></b><th id='Gs33T'></th></span></q></dt></tr></i><div id='Gs33T'><tfoot id='Gs33T'></tfoot><dl id='Gs33T'><fieldset id='Gs33T'></fieldset></dl></div>
                1. <legend id='Gs33T'><style id='Gs33T'><dir id='Gs33T'><q id='Gs33T'></q></dir></style></legend>
                2. <small id='Gs33T'></small><noframes id='Gs33T'>