L P
L P

Reputation: 1846

How to verify if a button is enabled and disabled in Webdriver Python?

I tried using the following from another post

driver.find_element_by_name("sub_activate").click().is_enabled()
but got this error:

AttributeError: 'NoneType' object has no attribute 'is_enabled'

Upvotes: 21

Views: 150403

Answers (6)

pramod_maddu
pramod_maddu

Reputation: 93

driver.find_element_by_name("sub_activate").click().is_enabled()

In the above method you have used click() method. Without using it you can verify whether a button is disabled or enabled. It returns a Boolean value.

Upvotes: 1

Prarna Dhar
Prarna Dhar

Reputation: 11

You Can try this one also:

Assert.assertTrue(driver.findElementById("Disable Element Id").isEnabled());

It worked fine in my case.

Upvotes: 0

Nisheeth
Nisheeth

Reputation: 281

The following works for me:

element = driver.find_element_by_name("sub_activate")
prop = element.get_property('disabled')
print (prop)

>>>> False

Returns 'true' if enabled 'element.get_property('enabled')

Upvotes: 10

user3748027
user3748027

Reputation: 31

IWebElement button = driver.FindElement(By.Id("ButtonId"));

Assert.AreEqual(false, button.Enabled); /*Validates whether the button is Disabled*/

Assert.AreEqual(true, button.Enabled); /*Validates whether the button is Enabled*/

Upvotes: 2

nerdwaller
nerdwaller

Reputation: 1863

You are calling is_enabled() on the click() result (None).

Instead, you should first get the element, check if it is_enabled() then try the click() (if that is what you are trying to do).

Take a look at the docs for the methods on the webelement.

is_enabled()
    Whether the element is enabled.

click()
    Clicks the element.

For example:

elem = driver.find_element_by_id("myId")
if elem.is_enabled():
    elem.click()
else:
    pass # whatever logic to handle...

Upvotes: 4

alecxe
alecxe

Reputation: 473863

You don't need to call click(). Just find the element and call is_enabled() on it:

element = driver.find_element_by_name("sub_activate")
print element.is_enabled()

FYI, click() is a method on a WebElement, it returns None.

Upvotes: 42

Related Questions