Selenium WebDriver get text from CSS property “content” on a ::before pseudo element

后端 未结 2 839
时光取名叫无心
时光取名叫无心 2020-12-01 07:16

I am trying to verify the text \"The information provided is either invalid or incomplete.\" is displayed using Selenium/WebDriver in Java.

I have the following HTML

相关标签:
2条回答
  • 2020-12-01 07:27

    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);
    

    This should print

    The information provided is either invalid or incomplete.
    
    0 讨论(0)
  • 2020-12-01 07:35

    Tried the same, works for me.

    Created sample html file :

    <!DOCTYPE html>
    <html>
    <head>
    <style>
     p::after { 
      content: " - Remember this";
    }
    </style>
    </head>
    <body>
    
    <p id='b'>My name is Donald</p>
    <p>I live in Ducksburg</p>
    
    </body>
    </html>
    

    Reused Nilesh's code

        String script  = "return window.getComputedStyle(document.querySelector('p#b'), ':after').getPropertyValue('content')";
        JavascriptExecutor js = (JavascriptExecutor) UIKeyWords.getUIDriver();
        String contentValue = (String) js.executeScript(script);
        System.out.println(contentValue);
    
    0 讨论(0)
提交回复
热议问题