0

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!")
8
  • Does button_xpath XPath locator is unique? Commented Aug 22, 2021 at 20:33
  • yes it's unique Commented Aug 22, 2021 at 20:35
  • Why do you use try-except here? The timeout period is long enough. It should work without it. Commented Aug 22, 2021 at 20:40
  • 1
    it just to handle if any error happen to avoid break script code Commented Aug 22, 2021 at 20:42
  • No, it makes your code wrong. Can you remove it and see if errors thrown? If yes - what error? Commented Aug 22, 2021 at 20:56

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

9 Comments

this is the button element <button type="button" class="jsx-2205086601 button btn-post primary disabled">Post</button>
@EL-AJIOussama I edit code based on button tag. Test with edited code
this is website tiktok.com/upload?lang=en when the button is ready the disabled attribute disappear <button type="button" class="jsx-2205086601 button btn-post primary">Post</button>
@EL-AJIOussama Then edited code will be done as your intent. Test and tell me it works or not
What do you mean
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.