Python Selenium Wait for user to click a button Python Selenium Wait for user to click a button selenium selenium

Python Selenium Wait for user to click a button


Q: How do wait for the user to click the button?

In this case , you can introduce WebDriverWait which is explicit wait in selenium.

You can try with this code :

from selenium.webdriver.support import expected_conditions as ECwait = WebDriverWait(driver, 10)element = wait.until(EC.element_to_be_clickable((By.ID, 'submitID')))  

Q. How do I then click OK on the dialog box?

In this case first you would have to switch the focus of your web driver to the alert and then you can click on it.

    alert = browser.switch_to.alert    alert.accept()    print("alert accepted")  

UPDATE 1:

When you are performing the click operation then there is one alert gets popped up. You can extract the text from the alert using this code :

alert = browser.switch_to.alertmsg_from_alert = alert.text  alert.accept() 

Now You can simply match it with your expected message which is already known to you.

expected_msg = "some msg from alert"  from collections import CounterCounter(msg_from_alert) == Counter(expected_msg)True


Here is a solution I devised that may not work for everybody. Poll the URL...

poll_rate = 1current_url = driver.current_urlwhile driver.current_url == current_url:  time.sleep(poll_rate)

Can anybody come up with a better solution?!

I am shocked that it is almost impossible to detect user input in a practical manner.