is_enabled() method in Selenium Python

As the name suggests, the is_enabled() method is used to check whether an element is enabled or not. This post will delve into the details of using the is_enabled() method on various elements.

  • Method declaration – def is_enabled(self) -> bool
  • What does it do? It returns true if an element is enabled. Otherwise, it returns false.

Let’s use the is_enabled() method on the highlighted element in the below image. This element is present on our Selenium playground website: https://testkru.com/Elements/Buttons.

Disabled button

We can use the find_element() method in Selenium Python to locate the element with the “disabledButton” id.

element = driver.find_element(By.ID, 'disabledButton')

Then, we can use the is_enabled() method to check whether the button is enabled or not.

element.is_enabled()

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/Buttons")

element = driver.find_element(By.ID, 'disabledButton')
print("Is element enabled:",element.is_enabled())

Output –

Is element enabled: False

We can see that the function returned False, indicating that the element is disabled. Now, let’s try on an element that is enabled at the moment.

We have an “enabled” button on the same page – https://testkru.com/Elements/Buttons.

Left click button

Again, we can use the element’s id to locate the element and then use the is_enabled() method on the located element.

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, 'leftClick')
print("Is element enabled:",element.is_enabled())

Output –

Is element enabled: True

So, now the function returns True, indicating that the element is enabled.

What if the element changes the state from enabled to disabled?

The “leftClick” element becomes disabled after a click operation as shown in the below images.

Before clicking on the button
After clicking on the button

Let’s check the button’s status before and after the click operation to verify whether the is_enabled() method returns the current state of the element.

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, 'leftClick')
print("Is element enabled before click:",element.is_enabled())

element.click()

print("Is element enabled after click:",element.is_enabled())

Output –

Is element enabled before click: True
Is element enabled after click: False

It can be seen from the above output that is_enabled() returns the current state of the element.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write 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 *