How To Handle Pseudo-Elements In CSS With Selenium?

Posted by Harshit Paul | June 1, 2021
Automation •

105944 Views | 14 Min Read

Pseudo-Elements In CSS With Selenium

While performing UI automation testing on a webpage, we all try to work with the web elements such as buttons, texts, etc. Interaction with the WebElements in the DOM is made possible with the help of Selenium locators. However, there are certain elements on a web page that even the locators can’t manage. Pseudo-elements in CSS fall in that category. Without the correct information, these pseudo-elements can be very hard to automate. In this article, we will see how to handle pseudo-elements in CSS with Selenium WebDriver.

Selenium is a powerful automation testing framework for checking complex web pages but pseudo-elements are tricky to automate. Selenium, when used in conjunction with the JavaScriptExecutor interface, helps to automate pseudo-elements. When testing your website, one must also ensure that the webpage functions as expected across various browser and OS combinations. Given that Selenium test automation is a cross-browser and cross-platform affair, you can perform automation tests at scale with the cloud-based Selenium Grid.

What are Pseudo-Elements in CSS?

The combination of two words – pseudo and elements – can often lead to a misunderstanding, so let’s understand what exactly pseudo-elements are in CSS (Cascading Style Sheet).

While using CSS on any web element, we usually apply a style all over it. But what if we want to apply a style only to a specific part of it and not to a complete element. I know what you are thinking.

Source

And the answer is YES. This is possible with the help of pseudo-elements. Pseudo-elements are a CSS component used to style specified parts of a web element. These pseudo-elements can be used to apply CSS on a portion or a specific part of a web element, such as:

  • Applying CSS on the first letter of an element
  • Applying CSS on the first line of an element
  • Inserting some text before the text of an element
  • Inserting some text after the text of an element

The pseudo-elements are defined with the help of a double colon (::), as shown in the below syntax:

Common types of Pseudo-Elements in CSS

Before starting to work on the pseudo-elements in Selenium, let us first see some commonly used types of pseudo-elements in CSS.

The ::first-line Pseudo-element

As the name suggests, the ::first-line pseudo-element in CSS is used to add special CSS styling only to the first line of the text and can be applied only to the block-level elements.

Syntax:

Pseudo-element in CSS example: ::first-line pseudo-element

Consider the below HTML code:

If you try to run the above HTML file, the output will be:

HTML file

As you can see, the desired Green color (#00ff00) is applied only on the first line of the <p></p> element, and that is possible only with the help of::first-line pseudo-element.

The ::first-letter Pseudo-element

As the name suggests, the ::first-letter pseudo-element in CSS is used to add special CSS styling only to the first letter of the text and can be applied only to the block-level elements.

Syntax:

Pseudo-element in CSS example: ::first-letter pseudo-element

Consider the below HTML code:

If you try to run the above HTML file, the output will be:

HTML file

As you can see, the desired Green color (#00ff00) is applied only on the first letter of the <p></p≶ element, and that is possible only with the help of ::first-letter pseudo-element.

The ::before Pseudo-element

As the name suggests, the ::before pseudo-element in CSS is used to add special CSS styling before the content of any element.

Syntax:

Pseudo-element in CSS example: ::before pseudo-element

Consider the below HTML code:

If you try to run the above HTML file, the output will be:

Pseudo-element

As you can see, the desired therefore symbol (∴) is applied before the <p></p> element, and that is possible only with the help of ::before pseudo-element.

The ::after Pseudo-element

As the name suggests, the ::after pseudo-element in CSS is used to add special CSS styling after the content of any element.

Syntax:

Pseudo-element in CSS example: ::after pseudo-element

Consider the below HTML code:

If you try to run the above HTML file, the output will be:

As you can see, the desired therefore symbol (∴) is applied after the <p></p> element, and that is possible only with the help of::after pseudo-element.

Similarly, there are many more pseudo-elements available in the CSS, such as:

The ::marker Pseudo-element

The ::marker pseudo-element in CSS is used to add special CSS styling to the list markers, such as unordered list bullet points, ordered list numbers, etc.

Syntax:

The ::selection Pseudo-element

The ::selection pseudo-element in CSS is used to add special CSS styling to the user-selected portion or content.

Syntax:

Backward Compatibility

Similar to pseudo-elements, there also exists a CSS pseudo-class. To define a pseudo-class, a single colon (:) is used.

Syntax:

But, you might come across situations where a single colon is used for both – the pseudo-class and the pseudo-elements. This is because, before CSS3, both the pseudo-class and pseudo-elements had the same syntax. In CSS3, a double colon (::) was introduced for pseudo-elements instead of a single colon (:). So you might see the single-colon syntax used for both pseudo-classes and pseudo-elements in CSS2 and CSS1.

Read – Ultimate CSS Selectors Cheat Sheet

For backward compatibility, the single-colon syntax is acceptable for CSS2 and CSS1 pseudo-elements. Consider the below table for a complete backward compatibility chart of pseudo-elements in CSS:

Browser

Lowest Version

Support of

Internet Explorer

8.0

:pseudo-element

9.0

:pseudo-element ::pseudo-element

Firefox (Gecko)

1.0 (1.0)

:pseudo-element

1.0 (1.5)

:pseudo-element ::pseudo-element

Opera

4.0

:pseudo-element

7.0

:pseudo-element ::pseudo-element

Safari (WebKit)

1.0 (85)

:pseudo-element ::pseudo-element

Source

Why Can’t Normal Locators Be Used To Automate Pseudo-Elements In Selenium?

You must be wondering why we can’t use the normal Selenium locators to automate pseudo-elements in CSS. To understand that, let us first go ahead and try to automate them using the Selenium locators.

Let us consider the following webpage:

In this, as you can see, we have used a ::before pseudo-element, which is adding the, therefore (∴) symbol before the text of the button (i.e., Submit).

Note: To help with the automation, I have uploaded this code in a sample GitHub repo.

So let us try to automate this element using the CSSSelector locator in JavaScript.

Code Walkthrough:

In the above code, the steps done are as follows:

First, the Selenium WebDriver is set up for the local Chrome browser.

Then the page where I have published the HTML code is opened in the local Chrome browser. You can use your local URL as well for this line.

Then the desired pseudo-element is being called with the help of CSS Selector, and the result is stored in the variable element.

Upon executing the findElement() method in the last step, the desired pseudo-element is called, and the result is stored in the variable element. It is then validated with the help of the then() method and printed to console in the following line.

driver.quit();

At last, the local browser is ended by destroying the Selenium WebDriver instance.

Expected Output:

Ideally, as per locators in Selenium, the above code should produce the value of the content property as output:

"∴ "

Actual Output:

Actual Output

Upon executing the test, you get the output as NoSuchElementError.

Why NoSuchElementError?

Although the locator is correct, you cannot work with the pseudo-elements with normal Selenium locators. This is because the pseudo-elements in CSS on a webpage are treated as a JavaScript element. It means that these pseudo-elements in CSS are executed in the front-end at runtime when the page loads and not initially. This is why when the Selenium WebDriver wants to interact with these pseudo-elements, the code gives NoSuchElementError.

Let Us Try In Java

Consider the following Java code, which tries to use the CSS Selector Selenium locator and get the value of pseudo-element.

Upon execution, the Java code also gives the NoSuchElementException.

NoSuchElementException

How To Work With Pseudo-Elements In Selenium?

Since the Selenium locators fail with pseudo-elements, the question arises that is there any way to work with the pseudo-elements in Selenium?

Source

Yes. The pseudo-elements can be automated in Selenium with the help of JavaScriptExecutor. By definition, JavaScriptExecutor is an Interface that helps to execute JavaScript through Selenium Webdriver. Since pseudo-elements in CSS are treated as JavaScript, hence we can use the JavaScriptExecutors to interact with them.

For example, to get the value of content property in the above code, the JavaScriptExecutor code will be:

Let us see via a complete code.

How To Work With Pseudo-Elements In Selenium JavaScript?

In case you are new to JavaScript with Selenium, do check out our detailed tutorial on Automating Testing with Selenium using JavaScript. Consider the following test written in JavaScript to run on local Google Chrome.

Code Walkthrough:

In the above code, the steps done are as follows:

First, the Selenium WebDriver is set up for the local Chrome browser.

Then the page where I have published the HTML code is opened in the local Chrome browser. You can use your local URL as well for this line.

The script to get the property value (content) of the class submitButton is written. It is then executed with the help of the driver.executeScript() method.

Upon executing the script in the last step, the desired pseudo-element is called, and the result is stored in the variable element. It is then validated with the help of the then() method and printed to console in the following line.

driver.quit();

At last, the local browser is ended by destroying the Selenium WebDriver instance.

Output:

Upon execution of the above test, the output will be:

Selenium WebDriver

As you can see, we have now received the ideal output:

"∴ "

How To Work With Pseudo-Elements In Selenium Java?

The JavaScriptExecutor can be used to work with pseudo-elements in Selenium Java. Consider the following test written in Java to run on local Google Chrome.

Code Walkthrough:

In the above code, the steps done are as follows:

First, the Selenium WebDriver is set up for the local Chrome browser.

Then the page where I have published the HTML code is opened in the local Chrome browser. You can use your local URL as well for this line.

The browser is then maximized using the maximize() method.

The script to get the property value (content) of the class submitButton is written. It is then executed with the help of the JavascriptExecutor.executeScript() method, and the value is retrieved as a String in the variable text.

System.out.print(text);

Upon executing the script in the last step, the desired pseudo-element is called, and the result is stored in the variable text. It is then printed to the console.

driver.quit();

At last, the local browser is ended by destroying the Selenium WebDriver instance.

Output:

Upon execution of the above test, the output will be:

As you can see, we have now received the ideal output.
"∴ "

Running Tests On Cloud-Based Selenium Grid

The solution to the problems or limitations of the local Selenium setup is the cloud-based Selenium Grid. One of such cloud-based Selenium Grid providers is LambdaTest. LambdaTest allows you to run your tests on its online cloud-based Selenium Grid on 2000+ browsers, browser versions, and operating systems with the help of Remote WebDriver.

To run Selenium test automation on LambdaTest Selenium Grid, you need:

  • A LambdaTest username and access key.
  • Selenium Desired Capabilities to run your tests on the desired combination of browsers, browser versions, and operating systems. These Selenium Desired Capabilities are language-specific and can be easily written with the help of LambdaTest Desired Capabilities Generator.

For example, let’s say the browser we want to test on is Firefox, version 89, and the operating system macOS Big Sur. Also, let’s try it at a resolution of 1280×960.

  • Browser: Firefox
  • Browser Version: 89
  • Operating System: macOS Big Sur
  • Resolution: 1280×968

So we can select the same capabilities from the capabilities generator.

capabilities generator

Let’s try to convert our local Selenium test automation setup code to execute on the LambdaTest platform.

Executing Java Code On LambdaTest

As the prerequisites, we first need to get the username, access key, and the desired capabilities.

  • To set the username and access key, replace yours from your Profile section in the below code snippet for Grid URL. Grid URL is your specific Remote WebDriver route on which the tests will be executed.
  • Next, get the above mentioned desired capabilities for Java from the Capabilities Generator:
  • We need to create the Remote WebDriver instance and initialize it with the above capabilities and Grid URL.

That’s it. Now we can use this Remote WebDriver instance to write and execute our test to show working on pseudo-elements in Selenium. Below’s the complete Java code:

Upon Selenium test automation execution, you can see the test getting run on the desired environment configuration on the LambdaTest platform. You can even refer to the test execution video to view the actual test happening.

LambdaTest platform

Executing JavaScript Code On LambdaTest

Similarly, let’s try to execute our JavaScript code on the LambdaTest platform. For a quick overview on automation testing with Selenium and JavaScript, do check out the below video from the LambdaTest YouTube channel.

As the prerequisites, we first need to get the username, access key, and the desired capabilities.

  • To set the username and access key, replace yours from your Profile section in the below code snippet for Grid URL. Grid URL is your specific Remote WebDriver route on which the tests will be executed.
  • Next, get the above mentioned desired capabilities for Java from the Capabilities Generator:
  • We need to create the Remote WebDriver instance and initialize it with the above capabilities and Grid URL.

That’s it. Now we can use this Remote WebDriver instance to write and execute our test to show working on pseudo-elements in Selenium. Below the complete JavaScript code:

Upon execution, you can see the test getting run on the desired environment configuration on the LambdaTest platform.

Conclusion

The UI of a webpage is often very complex, and to test the complete UI, Selenium automation is one of the most effective ways. You might even come across complex pseudo-elements in CSS, but the method to work with these pseudo-elements in Selenium will remain the same. You can use the JavaScript Executor to automate the pseudo-elements in any language.

Along with testing the UI, one must also ensure the cross browser compatibility of the webpage. Since the local Selenium test automation setup can’t be used to ensure that, the cloud-based Selenium Grid like LambdaTest is efficient.

We hope you learned how to work with pseudo-elements in Selenium.
Thank you.

Written by

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *