Confirm warning window in phantom.js

I have this function that finds a button and presses it, but after that a warning appears and I need to confirm it using phantom.js

function() { page.evaluate(function() { $('.item-list clicked').first().find($('.comment-delete')).find('a').click(); }) } 

Maybe I can emulate a function that triggers a warning without a click? or use the waitFor function to wait for this warning? (this is unlikely, waitFor waiting only for DOM objects, I think so)

+7
javascript web-scraping phantomjs confirm
source share
5 answers

As far as I know, Phantom.JS is headless. The only thing you can do is get the alert context with

 window.onAlert = function(alertText){ ... } 

but no more than that, I think. You cannot either close (in general) or visualize (in Phantom.JS) a software warning.

Some sources:
* Send JavaScript alerts using Phantom.JS
* Close warning programmatically

+3
source share

I cannot find another stackoverflow answer that helped me answer this, but basically you can enter a javascript function that will acknowledge the warning popup:

Here is my python webdriver implementation:

 def example_js_confirmation( self ): js_confirm = 'window.confirm = function(){return true;}' # .js function to confirm a popup self.execute_javascript( js_confirm ) self.find_by_id( 'submit' ).click() self.execute_javascript( 'return window.confirm' ) # trigger the injected js that returns true (virtually click OK) 

This is a todo =) list for someone: Selenium Desired Capabilities - set handles for the PhantomJS driver

+6
source share

โ€œConfirm Alertโ€ is a confusing phrase, because Javascript provides both alert (which displays a message with the OK button) and confirm (which displays the OK and Cancel buttons), and it does not show which one I have in mind.

At least PhantomJS 2.x behaves as if it clicked OK on alert and canceled in confirm .

If you want to click โ€œOKโ€ instead of โ€œCancelโ€ in confirm , you can call execute_javascript to override window.confirm to return true until you are concerned about the confirm windows that appear during the boot process, which will be reviewed before your execute_javascript will be able to intervene. If you want to catch them, the best I can come up with is to fix the PhantomJS source or enter JS through a proxy server.

0
source share

Using splinter in Python, you would like to add these lines (from docs ).

 alert = browser.get_alert() alert.accept() 
-one
source share

I have done this:

 // 1) override window.alert page.evaluate(function(){ window.alert = function(msg){ window.callPhantom({alert: msg}); // will call page.onCallback() return true; // will "close the alert" }; }); // 2) re-bind to onAlert() page.onCallback = function(obj) { page.onAlert(obj.alert); // will trigger page.onAlert() }; 
-one
source share

All Articles