is_displayed() method in Selenium Python

is_displayed() method in Selenium Python is used to check whether the element is visible to the user or not. This post will discuss the is_displayed() method in detail.

  • Method declaration – def is_displayed(self) -> bool
  • What does it do – The is_displayed() method checks if an element is visible or not. It returns true if the element is visible and false if not.
Code Examples
Using is_displayed() on a visible element

Let’s pick the highlighted element in the image below. This element is present on our playground website – https://testkru.com/Elements/TextMessages.

As we can see, the element is visible, so the is_displayed() method should return true.

Plain text element
  • The element id is “plainText.” We can use it to find the element using the find_element() method in Selenium Python.
element = driver.find_element(By.ID, 'plainText')
  • Then, use the is_displayed() method to determine whether the element is visible or not.
element.is_displayed()

Whole code

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

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True) # Using this, so the browser doesn't auto-close itself
driver = webdriver.Chrome(options)
driver.get("https://testkru.com/Elements/TextMessages")

element = driver.find_element(By.ID, 'plainText')
print("Is Element Displayed: " + str(element.is_displayed()))

Output –

Is Element Displayed: True

We can see that the is_displayed() method returned true. Let’s try a case where the element is not visible on the webpage.

Using is_displayed() on a hidden element

As highlighted in the image, we have one hidden element on the same webpage ( https://testkru.com/Elements/TextMessages ).

hidden element

In this case, we will use the id “hidden text” to locate an element and check if it is displayed using the is_displayed() method.

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

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True) # Using this, so the browser doesn't auto-close itself
driver = webdriver.Chrome(options)
driver.get("https://testkru.com/Elements/TextMessages")

element = driver.find_element(By.ID, 'hiddenText')
print("Is Element Displayed: " + str(element.is_displayed()))

Output –

Is Element Displayed: False

So, the is_displayed() method returned false this time.

Using is_displayed() method on a disabled element

There is often confusion regarding the output of the is_displayed() method for a disabled element. The method returns true, and this behavior is because it only checks for the element’s visibility, not its disabled status. To check if an element is disabled, you can use the is_enabled() method.

Let’s consider an example. We’ll use the is_displayed() method on the highlighted element on our playground website, https://testkru.com/Elements/Buttons.

Disabled button
from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True) # Using this, so the browser doesn't auto-close itself
driver = webdriver.Chrome(options)
driver.get("https://testkru.com/Elements/Buttons")

element = driver.find_element(By.ID, 'disabledButton')
print("Is Element Displayed: " + str(element.is_displayed()))

Output –

Is Element Displayed: True

We can see that the method returned true.

We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *