如何验证 Webdriver Python 中是否启用和禁用按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23498151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:02:55  来源:igfitidea点击:

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

pythonseleniumselenium-webdriver

提问by L P

I tried using the following from another post

我尝试使用另一篇文章中的以下内容

driver.find_element_by_name("sub_activate").click().is_enabled()
但得到这个错误:

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

采纳答案by alecxe

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

你不需要打电话click()。只需找到元素并调用is_enabled()它:

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

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

仅供参考,click()是 a 上的一个方法WebElement,它返回None.

回答by nerdwaller

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

您呼叫is_enabled()click()结果(无)。

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).

相反,您应该首先获取元素,检查它是否is_enabled()然后尝试click()(如果这是您想要做的)。

Take a look at the docsfor the methods on the webelement.

快来看看文档上的方法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...

回答by user3748027

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*/

回答by Nisheeth

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')

如果启用 'element.get_property('enabled'),则返回 'true'

回答by Prarna Dhar

You Can try this one also:

你也可以试试这个:

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

It worked fine in my case.

在我的情况下它工作得很好。

回答by pramod_maddu

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.

在上面的方法中,您使用了 click() 方法。如果不使用它,您可以验证按钮是否被禁用或启用。它返回一个布尔值。