Context:
confirm()
dialog
box asking the user "Do you want to submit the data"My intention:
My script would wait till the user click the button. Once it detects that the user clicked the button, my script would get a value of an element and then (somehow) click OK on the dialog box.
Question:
How do wait for the user to click the button?
How do I then click OK on the dialog box?
Additional Notes:
Using: chromedriver, Python 2.7
The button: <input id="submitID" type="button" class="medium" value="Submit filled Data">
[EDIT] Some code snippet:
The dialog popup is javascript popup:
if (window.confirm("Are you sure you want to submit the data?")) {
this.SaveData();
}
My code (simplified & modified for this question):
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
PATH_TO_CHROMEDRIVER = 'path/to/chromedriver'
URL = 'https://website-asking-user-to-fill-in-stuff.com'
driver = webdriver.Chrome(PATH_TO_CHROMEDRIVER)
driver.get(URL)
while True:
# loop until user close the chrome.
# If anyone has any better idea to
# WAIT TILL USER CLOSE THE WEBDRIVER, PLEASE SHARE IT HERE
try:
# --- main part of the code ---
waitForUserToClickButton() # this is what I need help with
driver.find_element_by_id('elementID').text
confirmJavascriptPopup() # this is also what I need help with
except WebDriverException:
print 'User closed the browser'
exit()
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 EC
wait = 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.alert
msg_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 Counter
Counter(msg_from_alert) == Counter(expected_msg)
True
Here is a solution I devised that may not work for everybody. Poll the URL...
poll_rate = 1
current_url = driver.current_url
while 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With