Skip to content
CodeKruPro

Read a CSS Property

24/80

Read a CSS Property

value_of_css_property returns the computed CSS value, such as visibility.

Task:

  1. Open Text Messages
  2. Read visibility for #plainText
  3. Read visibility for #hiddenText

Print the lines below exactly.

Expected output:

plainText visibility: visible
hiddenText visibility: hidden
Read full tutorial on Codekru

About this lesson

Read a computed style with value_of_css_property in Selenium Python

Selenium Python AssignmentsBeginnerWeb Element Interactions

How it works

value_of_css_property() asks the browser for the computed value of one CSS property on an element. Computed means the final result after every stylesheet, inline style and default has been applied. You pass the property name as a string, and you get a str back, for example the word that visibility ends up with.

This is not the same as reading the style attribute. On the hidden paragraph of the practice page, the style attribute holds a short inline rule. The computed value is what the browser actually uses, and it also covers properties you never wrote anywhere. A visible paragraph has no inline rule at all, yet the computed visibility is still a word.

Colors and sizes come back normalised. A color arrives as a string like rgba(52, 58, 64, 1), not a name or a hex code, and a bold weight arrives as the number 700 inside a string. A property name the browser does not know returns an empty string rather than raising an exception.

About the assignment

Compare two paragraphs on the Text Messages page, one that people can see and one that is hidden, by reading the same CSS property from each.

What you will do

  1. Load the Text Messages page and locate the plain paragraph and the hidden paragraph by id.
  2. Call value_of_css_property() on each element with the property that controls whether the element is shown.
  3. Print one line per element. Each line starts with the id of the element, then a label, then the answer.
  4. Keep the plain paragraph first and the hidden one second.

Common mistakes

  • Using get_attribute for style

    get_attribute("style") gives only the inline rule, slightly normalised, which for a visible element is an empty string. It does not tell you the computed result, so the two lines you compare can look the same when they are not.

  • Misspelling the property

    A name like visibilty does not raise anything. The call returns an empty string, and the output line ends right after the label. CSS names are lowercase with hyphens, for example font-weight.

  • Reading the inline style instead

    Hidden elements may carry a short inline rule, but visible ones carry none. Do not guess from that; ask for the computed value.

In a real test suite

Style checks are less common than text checks in a real suite, because visual details are fragile. Teams use them for state that has no other signal, such as a disabled look or an error color, and pair them with is_displayed() for visibility.

The next lessons move to checkboxes and radios, where the state you need is a true or false answer from is_selected().

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

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

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/TextMessages")
    # Print visibility for plainText and hiddenText
finally:
    driver.quit()
Read the full tutorial