How can I handle an alert with GhostDriver via Python?

对着背影说爱祢 提交于 2019-11-30 09:25:54

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!