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 Sahiti
• 6,380 points
5,545 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,460 points
The above code is working for taking single element value. how do take  list of elements for the same id using(validationError) how do we get the list of string from this. can you please explain. Thanks

Hi, @There,

Did you face any error while executing your code?

String script1 = "return window.getComputedStyle(document.querySelectorAll('.read-content').get("+i+"),':before').getPropertyValue('color')";
after execute this code shows me an error for get()
String scriptRead = "return window.getComputedStyle(document.querySelectorAll('.read-content',':before')).getPropertyValue('color')";
String contentRead= ((JavascriptExecutor)this.getDriver()).executeScript("arguments[0];",scriptRead.indexOf(i)).toString();
Is any wrong in this code... it returns null... Appreciate if anyone answer this. Thanks

Hey, @There,

Could you please post your error here so that you can resolve your error as soon a possible?

Related Questions In Selenium

0 votes
2 answers

Is there any way to get the text of a web element using Selenium Webdriver?

use gettext() in java : string lableText = ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,010 points
5,752 views
+1 vote
4 answers

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
• 28,040 points
18,640 views
0 votes
1 answer

Is there a way to get element by XPath using JavaScript in Selenium WebDriver?

You can use the document.evaluate: Evaluates an XPath ...READ MORE

answered Nov 26, 2020 in Selenium by Gitika
• 65,870 points
1,288 views
+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 mslavikas@gmail.com
21,965 views
0 votes
2 answers

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,520 points
5,087 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,821 views
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
3,591 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,010 points
4,330 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,460 points
10,001 views