Skip to content
Advertisement

Python selenium wait until button clickable

I have a python selenium code that upload a local video and click the button, the button can be clicked when video successfully uploaded so I try with EC.element_to_be_clickable and EC.visibility_of_element_located and when the button is clickable print a simple message, but always I got the message before video upload complete. this is my code :

file_uploader = driver.find_element_by_xpath('xpath_input')
file_uploader.send_keys(myvideo_file)
while True:
    try:
        WebDriverWait(driver, 1000).until(EC.element_to_be_clickable((By.XPATH, 'button_xpath')))
        WebDriverWait(driver, 1000).until(EC.visibility_of_element_located((By.XPATH, 'button_xpath')))
        break
    except:
        pass
print("Button ready to be clicked!")

Advertisement

Answer

What about using get_attribute of button.

If button is not clickable like <button type="button" class="btn-post primary disabled">Post</button>, then waiting until disabled disapeared in class name like <button type="button" class="btn-post primary">Post</button>

file_uploader = driver.find_element_by_xpath('xpath_input')
file_uploader.send_keys(myvideo_file)

while True:
    # I insert finding element inside while loop because some sites delete existing elements and create new ones.
    # In this case, even if the deleted element and new one's xpath are the same, StaleElementException occurs.
    class_name = driver.find_element_by_xpath('xpath_input').get_attribute('class')

    if 'disabled' not in class_name:
        break

    time.sleep(1)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement