Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 322
tsaulic Avatar asked Jun 04 '12 07:06

tsaulic


People also ask

What is alert () in driver switchTo ()?

//Using Alert class to first switch to or focus to the alert box. Alert alert = (Alert) driver. switchTo(). alert(); //Using accept() method to accept the alert box.

What is xOffset and yOffset in selenium?

Both xOffset and yOffset represent the relative pixel distance (integer) with which to scroll. For xOffset , positive value means to scroll right, and negative value means to scroll left. For yOffset , positive value means to scroll downward, and negative value means to scroll upward.

Can we automate graphs using selenium?

If you need to automate the generation of chart images or PDF, Selenium Webdriver might be the best option. It can handle different browsers even though this example only shows it using Firefox.


1 Answers

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

like image 132
tsaulic Avatar answered Oct 21 '22 00:10

tsaulic