How can I handle an alert with GhostDriver via Python?

前端 未结 2 636
北荒
北荒 2020-12-31 22:02

Problem: The GhostDriver API does not yet support alert handling. There is an acceptable workaround for the time being, which is to inject your own javascr

相关标签:
2条回答
  • 2020-12-31 22:46

    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.

    0 讨论(0)
  • 2020-12-31 22:56

    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")
    
    0 讨论(0)
提交回复
热议问题