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
- Find the paragraphs with ids
plainTextandhiddenText. - Call
getCssValue()with"visibility"on each one. - 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#343a40never matches. Convert withColor.fromString()fromorg.openqa.selenium.supportand compareasHex().Checking the wrong property for hidden
hiddenTextreportsdisplayasblock, because it is hidden withvisibility, notdisplay. Checking one property misses the other;isDisplayed()considers both and returnsfalsehere.Reading a hover style without hovering
The
hoverTextelement is dark grey until the mouse is over it, thenrgba(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.
Related lessons
- isDisplayed() - Check Element Visibility
isDisplayed()combines the visibility checks you would otherwise do by hand. - Mouse Hover ActionHover over an element, then read the style it changes to.
- Get Attribute Value with getAttribute()Attributes and properties, compared with computed styles.
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.
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();
}
}