简体   繁体   English

如何使用 Python Selenium 验证禁用的按钮

[英]How to verify a disabled button using Python Selenium

HTML of the button:按钮的 HTML:

 <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.在 WebDriver 中有一个名为is_enabled的 function 如果元素启用则返回 true,否则返回 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这是 selenium py 文档中的参考

"disabled" is not a property , but attribute so you need to replace "disabled"不是属性,而是属性,因此您需要替换

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. disabled属性是 boolean 属性,它指定除非满足某些先决条件,否则应禁用该元素。 A disabled element is unusable. disabled的元素不可用。 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.通常, disabled属性可以设置为阻止用户使用元素,直到满足某些其他条件,例如选择复选框、单选按钮等。


This usecase这个用例

As per the given HTML:根据给定的 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:要探测<button>是否被禁用,您可以执行以下测试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 验证是否在Python + Selenium中禁用了按钮 - Verify if a button is disabled in Python + Selenium 如何验证Webdriver Python中是否启用和禁用了按钮? - How to verify if a button is enabled and disabled in Webdriver Python? 如何使用 Python Selenium 管理具有禁用属性的按钮? - How to manage a button with disabled attribute using Python Selenium? 当类更改时,如何验证按钮是否可以在 Python 中使用 Selenium 进行单击? - How to verify if a button is clickable with Selenium in Python when the class changes? Selenium:如何点击禁用按钮 - Selenium: How to click on a disabled button 在使用.clear()或.send_keys()后禁用按钮时,如何使用Python Selenium单击(绕过)提交按钮? - How can I click (bypass) a submit button using Python Selenium, when the button gets disabled after using .clear() or .send_keys()? 如何使用 Selenium 和 Python 定位复选框以验证其状态 - How to locate a checkbox to verify its state using Selenium and Python 如何使用 Python / Selenium Webdriver 断言/验证成功登录 - How to assert/verify successful login using Python / Selenium Webdriver 如何使用python硒验证验证错误消息的属性文本? - How to verify attribute text for a validation error message using python selenium? 如何使用python从硒中的禁用输入中获取文本值? - how to get text value from disabled input in selenium using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM