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
1,525 views

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

This should print:

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

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
+1 vote
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
895 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,090 points
568 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
896 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
561 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,420 points
3,619 views