0

I am using python 2.7, Selenium, and PhantomJs.

When I click the sign in button in my script it goes to a new page and a popup appears that I must accept.

Here is the code I am trying to use. I got it from here How can I handle an alert with GhostDriver via Python?

sign_in.click() 
js = 'window.alert = function(message) { lastAlert = message; }'
driver.execute_script("%s" % js) 
driver.execute_script("return lastAlert")

Here is the error that I get:

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: {"errorMessage":"Can't find variable: lastAlert","request":{"hea ders":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"95","Content-Type ":"application/json;charset=UTF-8","Host":"127.0.0.1:56712","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","metho d":"POST","post":"{\"sessionId\": \"3832e2c0-4902-11e6-b766-0d7f487d0794\", \"args\": [], \"script\": \"return lastAlert \"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative": "/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","q ueryKey":{},"chunks":["execute"]},"urlOriginal":"/session/3832e2c0-4902-11e6-b766-0d7f487d0794/execute"}} Screenshot: available via screen

I am not very experienced with javascript and a point in the right direction would be useful.

Community
  • 1
  • 1

1 Answers1

0

Here is the Behat step that I'm using:

/** 
  * This step overrides windows.confirm and basically accepts it before it is displayed.
  *
  * @When /^(?:|I )bypass the popup$/
  */
  public function bypassPopup() {
    $function = "
    var realConfirm=window.confirm;
    window.confirm=function(){
    window.confirm=realConfirm;
    return true;
};
    ";
    $session = $this->getSession();
    $session->executeScript($function);
  }

You place it before you click on the button and when you click your popup will be accepted automatically. In this case, only the first popup will be accepted.