submit Button def isButtonDisabled(self): element = self.element" /> submit Button def isButtonDisabled(self): element = self.element" /> submit Button def isButtonDisabled(self): element = self.element"/>

How to verify a disabled button using Python Selenium

1.2k Views Asked by At

HTML of the button:

<button class="Button1" disabled type="button" xpath="1">submit Button</button>

def isButtonDisabled(self):
    element = self.element.findElement(By.xpath, 'locator')
    return element.get_property('disabled')

But this method is not working.

3

There are 3 best solutions below

0
MrKioZ On

There is a function in WebDriver called is_enabled which returns true if the element is enabled, else it returns false.

def isButtonDisabled(self):
    element = self.element.findElement(By.xpath, 'locator')
    return element.is_enabled()

Here's a Reference in the documentation of selenium py

0
Curious koala On

"disabled" is not a property, but attribute so you need to replace

element.get_property('disabled')

with

element.get_attribute('disabled')
0
undetected Selenium On

disabled Attribute

The disabled attribute is a boolean attribute which specifies that the element should be disabled unless some prerequisites are met. A disabled element is unusable. Generally the disabled attribute can be set to keep a user from using the element until some other condition has been met e.g. selecting a checkbox, radio button, etc.


This usecase

As per the given HTML:

<button class="Button1" disabled type="button" xpath="1">submit Button</button>

To probe if the <button> is disabled you can perform the following test:

try:
    self.element.findElement(By.xpath, '//button[text()="submit Button"][disabled]')
    print("button is disabled")
except NoSuchElementException:
    print("button wasn't disabled")