Skip to content
CodeKruPro

Get CSS Value with getCssValue()

21/70

Video walkthrough

Watch on YouTube

Get CSS Value

getCssValue() returns the computed CSS property value of an element. Common properties: color, background-color, font-size, visibility, display.

On the Text Messages page:

  • id="plainText" - visible text
  • id="hiddenText" - has visibility: hidden

Task:

  1. Navigate to https://testkru.com/Elements/TextMessages
  2. Get the visibility CSS property of plainText
  3. Get the visibility CSS property of hiddenText

Expected output:

plainText visibility: visible
hiddenText visibility: hidden

About this lesson

getCssValue in Selenium Java: read an element's computed CSS style

Selenium Java AssignmentsBeginnerWeb Element Interactions

How it works

getCssValue() takes a CSS property name and returns its computed value: the final result after stylesheets, inline styles, inheritance and the browser's defaults are applied. It does not read the style attribute. The plain paragraph on Text Messages has no style attribute, yet getCssValue("visibility") still returns a value for it.

Values come back in the browser's normalised form, not as they were written. Colours are rgba(...) strings, for example rgba(52, 58, 64, 1) for the paragraph text here, sizes are in pixels such as 14px, and font weights are numbers, so the strong element reports 900 rather than "bold".

Use longhand property names. Shorthands like margin or border may come back as a combined string or empty depending on the browser, while margin-top always gives one value. An unknown property name returns an empty string, not an exception.

About the assignment

Text Messages has a normal paragraph and one hidden with visibility: hidden. Both are in the DOM, so locating them works the same. You read the computed visibility of each to see how CSS state differs from DOM presence.

What you will do

  1. Find the paragraphs with ids plainText and hiddenText.
  2. Call getCssValue() with "visibility" on each one.
  3. Print one line per element in the order and format the task shows, with the element id first.

Common mistakes

  • Comparing colours against hex values

    Chrome returns colours as rgba(...), so an assertion against #343a40 never matches. Convert with Color.fromString() from org.openqa.selenium.support and compare asHex().

  • Checking the wrong property for hidden

    hiddenText reports display as block, because it is hidden with visibility, not display. Checking one property misses the other; isDisplayed() considers both and returns false here.

  • Reading a hover style without hovering

    The hoverText element is dark grey until the mouse is over it, then rgba(255, 165, 0, 1). getCssValue() only reports the current state, so move the mouse there with Actions first (lesson 51).

In a real test suite

Style checks are useful for states that only show through CSS: a field border turning red on a validation error, a disabled-looking button, or the active tab in a menu. For full visual layout, teams pair these with screenshot comparison instead.

Lesson 20 read attributes, and this lesson read computed styles. Lesson 22 starts on form controls with state of their own, beginning with selecting a checkbox.

To try one of these calls on any page without a task, open the online Selenium compiler for Java.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/TextMessages");
        
        // TODO: Get visibility CSS value for plainText and hiddenText
        
        driver.quit();
    }
}