Home > Back-end >  How to manage a button with disabled attribute using Python Selenium?
How to manage a button with disabled attribute using Python Selenium?

Time:02-10

Having this button:

<button id="btn-login-5" type="button"  disabled="">Update</button>

I want to remove the disable attribute in order to click the button.

This is the code i see everytime:

button = self.driver.find_element(
        By.XPATH, "/html/body/div/div/button")
self.driver.execute_script(
        'arguments[0].removeAttribute("disabled");', button)

But I can't figure me out how that can work for anyone, I mean, if the element is disabled, selenium cannot run that very first line, is not able to asign the var "button" because he can't find that element.

Am I missing something? I am getting this error if a run that:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/button"}

Update

I just realized this is related to selenium grid, I tried to click the button in a lot of ways, and no one works, but if instead of using selenium grid I use a local webdriver all of them works! So I have no idea what to do now.

CodePudding user response:

If you use this relative xpath, instead of absolute one, it works:

button = driver.find_element(By.XPATH, "//button[@type='button']")

But is this the only button in your DOM? If not, you may have to add other relative entities to it.

Refactored Code (as per your query info provided):

button = driver.find_element(By.XPATH, "//button[@type='button']")
driver.execute_script('arguments[0].removeAttribute("disabled");', button)
button.click()

Output:

Process finished with exit code 0

CodePudding user response:

The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the button clickable again.


You were close enough. To remove the disabled attribute, the Update element can be still identified for it's presence using the following Locator Strategy:

button = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, "//button[@class='m-1 btn btn-warning' and text()='Update']")))
self.driver.execute_script('arguments[0].removeAttribute("disabled");', button)

CodePudding user response:

All works fine, the problem is a different one, my code couldn't find the element because the element was not there, something very strange, I have a docker with the page server and if I open the page I can see the button but the selenium grid webdriver doesn't load the button at all, I don't know why, maybe that button depends of some server wich is not accesible from the webdriver.

I'll ask the page developer for this, but the code was good :)

I lost a full day of work for nothing :)

CodePudding user response:

If it's automated tests that fail, running with a --headless option, you could enable print-screens in case of errors and then check if the button shows or not. Might be that the button is outside of the viewport. Or the automated testing does not wait long enough for the element to be rendered.

  • Related