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 Sahiti
• 6,290 points

1 answer to this question.

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,460 points

Related Questions In Selenium

0 votes
1 answer
+1 vote
1 answer

How to get typed text from a textbox by using Selenium Webdriver?

Hey Ashmita, to get the typed text ...READ MORE

answered Jun 24, 2019 in Selenium by Abha
• 27,930 points
+1 vote
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 May 16, 2019 in Selenium by
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

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,510 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,890 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,890 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,460 points