How to Verify If a Button Is Enabled and Disabled in Webdriver Python

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

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.

Verify if a button is disabled in Python + Selenium

Try this:

element = driver.find_element_by_id("edit-save-m")
print element.is_enabled()

Prints true or false, based on element visibility.

How to verify if a button from a page is really disabled in a chromedriver window on Python? Selenium related

Ok, here it is:
First: The locators are not relative enough, so I took the freedom to refactor them.
Second: MetaMask is not a button as per DOM DOM Snapshot but the rest are. However, instead of relying on the buttons, I tried to rely on the li (which is common for all), and checking whether they are enabled or not.
Third: If you are trying to find if all the 4 elements are enabled, then you must use find_elements instead of find_element and then loop through the elements to check if each is enabled.
Please check if this works for you:

driver.get('https://opensea.io/') #go to the opensea main page.
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, "//*[@title='Wallet']"))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, "//*[@title='Wallet']")
wallet_button.click() #click that wallet button
wallet_options = driver.find_elements(By.XPATH, "//*[@data-testid='WalletSidebar--body']//li")
for wallet in wallet_options:
if wallet.is_enabled():
print(f"{wallet.text} is enabled")
else:
print(f"f{wallet.text} is not enabled")

Selenium (Python) - click on disabled button after it is enabled

Regarding the current version of your code, I think you may be right that it is clicking the button before it is really enabled. You have

WebDriverWait(browser, 15).until(EC.element_to_be_clickable((By.ID,"upload-button"))).click()

You are waiting for this element to be clickable. I wanted to try and figure out exactly what this meant so I looked at the source code. element_to_be_clickable is satisfied As soon as the element is "visible" and "enabled".

Visibility, I know, is defined as presence on the DOM and height/width both > 0. From your description it sounds like your button is immediately visible. So as soon as it is "enabled", element_to_be_clickable is satisfied and the wait will end.

This begs the question, what exactly determines whether an element is "enabled"? I found that selenium's is_enabled (which is required in the source code for element_to_be_clickable to pass), is essentially a negation of the W3C specification for disabled(). What it boils down to is this single line, which states that an element is "disabled" if The element is a button, input, select, textarea, or form-associated custom element, and the disabled attribute is specified on this element (regardless of its value).

That's it. Your element does have the "disabled" attribute, but it also has some other stuff that might cause it to be disabled -- the class name contains is-disabled, it's got aria-disabled="true" as well as data-is-focusable="false", all of which change by the time the button is fully clickable. I wonder if the disabled attribute goes away before something else that also causes the element to be disabled, so just as you said maybe your click is registering before the button is ready. To debug this I would try temporarily adding a hard wait, a few seconds long, after executing the WebDriverWait and before clicking the button.

For your class name,

WebDriverWait(browser, 15).until(EC.element_to_be_clickable((By.CLASS_NAME,"ms-Button ms-Button--primary root-437"))).click()

I suspect this is a dynamic class name, the root-437 part in particular, so maybe that's why that didn't work.

Finally, are you intending to upload from your filesystem by clicking the button? Because it can only interact with your web browser and can't browse a window on your OS, that doesn't work. There's a special way to upload files--you have to identify the file input element and send the absolute path of the file you want to upload to that element using send_keys().

Checking if a button is disabled or not

The documentation for isEnabled.

Sadly, using the isEnabled method doesn't work in this case, as stated by the documentation:

This will generally return true for everything but disabled input elements.

A proper alternative is using JavaScript to check for the attribute's existence, and its value. You can inject JavaScript through the executeScript method of the webdriver classes. The first argument is the script, all following arguments are passed to the script, accessible as arguments[i], ...

For example:

Boolean disabled = driver.executeScript("return arguments[0].hasAttribute(\"disabled\");", element);

How to detect button that is disabled and enabled with selenium?

Define the add_to_cart_button button with this selector button.addToCartButton

And you can check the attribute value with .get_attribute('class'):

add_to_cart_button = browser.find_element_by_css_selector('button.addToCartButton')

if "disabled" in add_to_cart_button.get_attribute('class'):
browser.refresh()
print("Item out of stock")
continue
else:
add_to_cart_button.click()
print("Item in stock")

Selenium thinks button is clickable when it's disabled and raises WebDriverException

Refreshing the webpage multiple times wouldn't ensure that the element would turn clickable instantly on page load. Modern websites are increasingly implementing JavaScript, ReactJS, jQuery, Ajax, Vue.js, Ember.js, GWT, etc. to render the dynamic elements within the DOM tree. Hence to invoke click() on a WebElement there is a necessity to wait for the element and its children to completely render and become interactable.



element_to_be_clickable()

element_to_be_clickable is the expectation for checking an element is visible and enabled such that you can click it. It is defined as:

def element_to_be_clickable(locator):
""" An Expectation for checking an element is visible and enabled such that
you can click it."""
def _predicate(driver):
element = visibility_of_element_located(locator)(driver)
if element and element.is_enabled():
return element
else:
return False

return _predicate

So ideally, to wait for the button to be clickable you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following xpath based Locator Strategy:

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[2]/div[2]/div[2]/div[2]/div[3]/div/div[5]/div/div/button[2]"))).click()

Note: You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


References

You can find a couple of relevant detailed discussions in:

  • Selenium identifies the button as clickable when it wasn't
  • Get HTML source of WebElement in Selenium WebDriver using Python

how to know if a button is enabled using selenium?

Just add an answer refers to the comment with a bit explanation.

You can check the attribute value by .get_attribute('disabled') method.

And I think you should use .find_element* (without s) not .find_elements*, because find by id is unique:

disable_val = driver.find_element_by_id('join_button_input').get_attribute('disabled')
if disable_val == 'true':
#perform
....
....

python selenium to check if this text field is disabled or not

You can use is_enabled() method of web driver.

Python

driver.find_element_by_name("group_name").is_enabled

Java

driver.findElement(By.name("")).isEnabled();

It will return true if its enabled otherwise false.



Related Topics



Leave a reply



Submit