8

Context:

  1. My script launch to a website using selenium webdriver
  2. The user fills in some stuff on the website
  3. The user will click a button which will popup a 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()
3
  • Is it a javascript or html popup? Also, you should share some code snippet for what have you tried so far and html for the alert if its an html popup Commented Aug 4, 2018 at 2:16
  • It's very interesting question but I think it's not possible via Selenium. Commented Aug 4, 2018 at 5:04
  • @theGuy It's a javascript popup. Commented Aug 4, 2018 at 10:12

3 Answers 3

5

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

14 Comments

EC.element_to_be_clickable will wait until the button will be clickable and unfortunately, the button is clickable all the time. But as for the second answer, it worked.
Element to be clickable checks for visibilty of web element and whether web element is enable or not. By combining these two conditions we assume that element should be in a state of clicking. Now if there is any problem with overlying that has to be handled by script writer.
Oh, I think you misunderstood my problem. I want the script to wait/detect when the user clicks or clicked the button and not when the button is clickable.
Ok what happens manually when you perform click operation ? I guess alert appeared after that ?
Yes, if you are thinking to use WebDriverWait(driver, 10).until(EC.alert_is_present()) then I already thought about it. (check my latest edit)
|
1

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.

Comments

0

Just a slight modification to the above answer for my use case. I put a 30 seconds window for the user to enter his password. The while loop is not necessary for my scenario.

poll_rate = 30
current_url = driver.current_url
time.sleep(poll_rate)
driver.find_element(By.NAME, "verifyPassword").click()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.