About this lesson
Selenium Python is_displayed: check if an element is visible on the page
Selenium Python AssignmentsIntermediateElement States
How it works
Finding an element and seeing an element are different things. find_element succeeds for anything in the DOM, including content hidden by CSS. is_displayed() checks CSS and layout for hidden states, but an element covered by another still counts as displayed, and returns a Python bool.
Unlike text and tag_name, is_displayed is a method and needs brackets. Without them you get a bound method object, which Python treats as true in an if. A check like that passes for hidden elements and nothing warns you.
On the Text Fields page, #invisibleField is an input with visibility: hidden. It is found without trouble, is_displayed() is False, and its text is empty. Typing into it raises ElementNotInteractableException ("element not interactable"). The method also reports False for display: none.
Two elements on the page share the id firstName. Selenium returns the first in page order. Both are visible inputs here. For the second, the simplest route is find_elements and an index.
Joining a boolean to a label needs str(). Without it Python raises TypeError with the message can only concatenate str (not "bool") to str.
About the assignment
Find two fields on the Text Fields page and report whether each is displayed. One is an ordinary input and one is hidden.
What you will do
- Locate the first
firstNamefield. The id appears twice, andfind_elementgives you the first. - Locate the hidden field by its id.
- For each, call the display check and convert the result with
str(). - Print one line per field, each with its own label from the task.
Common mistakes
Leaving off the brackets
is_displayedwithout()is a method object, not a bool. Always call it.Expecting NoSuchElementException for a hidden field
Hidden elements are in the DOM, so the find call works. The exception only appears when the id is not on the page at all.
Joining True to a string
Python will not add a bool to a
str. TheTypeErrornames both types. Wrap the call instr()or use an f-string.
In a real test suite
Suites use this for things like an error banner that should not show yet. Many teams put it in a helper that wraps a wait, because a visible element may take a moment to arrive.
The next lesson asks whether a field accepts input, and a later one whether a checkbox is ticked.
Related lessons
- Check Whether a Text Field Is EnabledAsk whether a field is enabled, a separate question from whether it is visible.
- Two Elements Share One IDPractise
find_elementon a page where one id appears twice. - Wait Until an Element Is VisibleWait for an element that starts hidden and becomes visible later.
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/TextFields")
# Print is_displayed for firstName and invisibleField
finally:
driver.quit()