简体   繁体   中英

How to verify a disabled button using Python Selenium

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.

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

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

element.get_property('disabled')

with

element.get_attribute('disabled')

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 eg 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM