tsaulic
tsaulic

Reputation: 717

Selenium/WebDriver script gets interrupted by alert - exception "Message: u'Modal dialog present'"

I'm fairly new at Python/JS and also automated testing with Selenium/WebDriver, but I have made some progress!

Now I'm stuck at one point and it's really frustrating.

The website I am testing sells products. I managed to make my script navigate randomly and get to the payment page, fill in dummy data, submit data by using:

browser.execute_script("document.Form.submit(); return true;")
browser.execute_script("processPayment(); return true;")

Usually, there is a Pay now button and clicking that element results in the same exception and there was no way for me to click OK/Cancel on it via WebDriver (no WebElement), but I figured out that executing this JS code I can get past it. My newly loaded page (after submitting data and confirming the posting of it) with a confirmation and all correct data loads, but the Python script is interrupted and I can't continue the test.

Is there a workaround for this? What I want it to do is to ignore that modal dialog, wait for the next confirmation page to load and then continue locating elements, printing their values, storing them etc.

Tried using:

wait = ui.WebDriverWait(browser,10)
wait.until(lambda browser: browser.title.lower().startswith('Your Receipt'))
print(browser.title)

but the script gets interrupted. Sorry if this has been answered, but I couldn't find it, and also I am a newbie!

Thanks in advance!

EDIT:

Did it! In my case what worked is I just modified my code a little

browser.execute_script("document.roomBookingForm.submit(); return true;")
alert = browser.switch_to_alert()
alert.dismiss()
browser.execute_script("processPayment(); return true;")

Note for newbies that you will need to import Alert.

from selenium.webdriver.common.alert import Alert

Upvotes: 11

Views: 11706

Answers (1)

tsaulic
tsaulic

Reputation: 717

Note for newbies (like me) that you will need to import Alert.

from selenium.webdriver.common.alert import Alert

... ... ... (code placeholder)

browser.execute_script("document.roomBookingForm.submit(); return true;")
alert = browser.switch_to_alert()
alert.dismiss()
browser.execute_script("processPayment(); return true;")

just added the alert handler

Upvotes: 9

Related Questions