confirm alert window in phantom.js

Alice Polansky picture Alice Polansky · Nov 11, 2013 · Viewed 11.4k times · Source

i have this function which find button and click it, but after that alert appears and i need to confirm it using phantom.js

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

may be i can emulate function which call alert whithout click immediately ? or use function waitFor for waiting this alert?(unlikely this, waitFor waiting only for DOM objects i think so)

Answer

foupfeiffer picture foupfeiffer · Nov 14, 2013

I can't find the other stackoverflow answer that helped me answer this as well but basically you can inject a javascript function that will confirm the alert popup:

Here's 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)

It's on somebody's todo list =) : Selenium Desired Capabilities - set handlesAlerts for PhantomJS driver