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
- Load the Text Messages page and locate the plain paragraph and the hidden paragraph by id.
- Call
value_of_css_property()on each element with the property that controls whether the element is shown. - Print one line per element. Each line starts with the id of the element, then a label, then the answer.
- 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
visibiltydoes 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 examplefont-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().
Related lessons
- Read Visible TextReads the same paragraphs with
.text, and the hidden one gives an empty string there. - Check Whether an Element Is DisplayedChecks
is_displayed(), the usual first check before reading hidden style. - Read an AttributeReads attributes, which is the closest alternative to a style read.
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.
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()