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">
    <ul>
        <li>Required: Server Name</li>
        <li>Required: Receiving Port</li>
    </ul>
</div>


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

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

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
760 views

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);
System.out.println(content);

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
2 answers

Is it possible to scroll down in a webpage using selenium webdriver programmed on python?

I using next code for facebook for ...READ MORE

answered 5 days ago in Selenium by mslavikas@gmail.com
4,199 views
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
64 views
0 votes
1 answer
0 votes
1 answer

Get text of a webelement in selenium

You can use the following statement to ...READ MORE

answered Feb 13 in Selenium by Priyaj
• 56,100 points
19 views
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,030 points
57 views
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
421 views
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
44 views
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
1,782 views

© 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.