Responding to confirm pop ups - OK or Cancel
The onConfirm
function in JavaScript displays pop ups which ask the user for an action that is answerable by either yes or no. The onConfirm
function callback allows us to respond to this pop-up dialog box.
page.onConfirm = function(msg) { return true; };
The callback function has a single parameter, which contains the question being asked to the user. We respond to this pop up by returning either true
or false
. For example, we want to click the OK
button, then we return true
; otherwise we return false
.
There are several more callback functions that you can use to listen and handle page events within PhantomJS. We can also catch error messages that are encountered during running the script. We will learn more about error handling in the next chapter.