Skip to content
CodeKruPro

Check Whether an Element Is Displayed

35/80

Check Whether an Element Is Displayed

is_displayed() is false when the element is in the page but not shown. #invisibleField uses visibility: hidden.

Task:

  1. Print whether the first #firstName is displayed
  2. Print whether #invisibleField is displayed

find_element returns the first #firstName. There are two.

Print the lines below exactly.

Expected output:

firstName displayed: True
invisibleField displayed: False
Read full tutorial on Codekru

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

  1. Locate the first firstName field. The id appears twice, and find_element gives you the first.
  2. Locate the hidden field by its id.
  3. For each, call the display check and convert the result with str().
  4. Print one line per field, each with its own label from the task.

Common mistakes

  • Leaving off the brackets

    is_displayed without () 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. The TypeError names both types. Wrap the call in str() 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.

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/TextFields")
    # Print is_displayed for firstName and invisibleField
finally:
    driver.quit()
Read the full tutorial