How can I handle a warning using GhostDriver via Python?

Problem: The GhostDriver API does not yet support alert handling. There is still an acceptable workaround, which is to insert your own javascript into the page, which will handle the warning and store it for you.

I am having problems using this workaround through python webdriver bindings. This may be due to an understanding of javascript at the beginner level.

Here is an example of a workaround I'm trying to use: https://github.com/detro/ghostdriver/issues/20#issuecomment-15641983

I am using a public site showing a warning to make this simpler: http://www.tizag.com/javascriptT/javascriptalert.php

Here is my code:

from selenium import webdriver button_xpath = "/html/body/table[3]/tbody/tr/td[2]/table/tbody/tr/td/div[4]/form/input" js = """ (function () { var lastAlert = undefined; window.alert = function (message) { lastAlert = message; }; window.getLastAlert = function () { var result = lastAlert; lastAlert = undefined; return result; }; }()); """ driver = webdriver.PhantomJS() driver.get('http://www.tizag.com/javascriptT/javascriptalert.php') driver.execute_script("window.alert = %s" % js) driver.find_element_by_xpath(button_xpath).click() #exception just occured driver.execute_script("return window.getLastAlert && window.getLastAlert();") 

The exception is:

 WebDriverException: Message: u'Error Message => \'Click failed: TypeError: \'undefined\' is not a function (evaluating \'alert(\'Are you sure you want to give us the deed to your house?\')\')\'\n caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:41752","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"sessionId\\": \\"0eaf7680-9897-11e2-b375-55b9cb6ceb0f\\", \\"id\\": \\":wdc:1364578560610\\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/0eaf7680-9897-11e2-b375-55b9cb6ceb0f/element/%3Awdc%3A1364578560610/click"}' ; Screenshot: available via screen 

I'm JO noob. I hope someone can point me in the right direction.

+6
source share
2 answers

A simple solution is to rewrite the window.alert method to output the argument of the 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 via execute_script call in python like this:

 driver.execute_script("%s" % js) 

Then, when all this starts, you can return in the global lastAlert:

 driver.execute_script("return lastAlert") 
+10
source

This is a workaround.

Use this for every page you reload that will have a warning later.

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

This works for PhantomJS inside Selenium / Splinter.

See link here .

+4
source

All Articles