Get text from CSS property “content” on a - before pseudo element in Selenium WebDriver

0 votes

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

I have the following HTML:

<div id="validationError">
        <li>Required: Server Name</li>
        <li>Required: Receiving Port</li>

CSS code is as follows:
#validationError:before {
    content: 'The information provided is either invalid or incomplete.';

Java Code:

WebElement errorBox = browser.findElement("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());

The output of the above code is shown below:

Error Explanation:

Error: Required: Server Name
Error: Required: Receiving Port

Error Full Text: Required: Server Name
Required: Receiving Port

I can't 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. how to solve this?

Aug 8, 2018 in Selenium by darklord
• 6,140 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I am not sure if WebDriver can retrieve pseudo element content or not. I think you would need to use Javascript. 

Below code worked for me:

String script = "return window.getComputedStyle(document.querySelector('#validationError'),':before').getPropertyValue('content')";
JavascriptExecutor js = (JavascriptExecutor)driver;
String content = (String) js.executeScript(script);

This should print:

The information provided is either invalid or incomplete.
answered Aug 8, 2018 by Meci Matt
• 9,400 points

Related Questions In Selenium

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer

How to get the text from a website using selenium?

hey, you can use get.text() method to ...READ MORE

answered Dec 31, 2018 in Selenium by Sudhanshu
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,050 points
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
0 votes
1 answer
0 votes
1 answer

Clicking on hidden element in Selenium WebDriver

let us first store that element in the ...READ MORE

answered Jun 12, 2018 in Selenium by Meci Matt
• 9,400 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.