How can I handle an alert with GhostDriver via Python? How can I handle an alert with GhostDriver via Python? selenium selenium

How can I handle an alert with GhostDriver via Python?


A simple solution is to rewrite the window.alert method to output the argument to a global variable.

Define the js injection variable with your override function:

js = """window.alert = function(message) {lastAlert = message;}"""

And then just pass the js variable through the execute_script call in python like this:

driver.execute_script("%s" % js)

Then when it's all been run you can execute a return on the global lastAlert:

driver.execute_script("return lastAlert")


This is some workaround.

Use this for every reloaded page that would have an alert later.

driver.execute_script("window.confirm = function(){return true;}");

This works for PhantomJS within Selenium/Splinter.

See more reference here.