Answering prompt messages

Aside from alert messages, there are prompt messages that ask users to input their answer into a single text field. To handle this kind of dialog box from web pages, PhantomJS allows us to handle and answer back to this dialog prompts using the onPrompt callback.

page.onPrompt = function(message, defaultVal) {
  if (message === 'How old are you?') {
    return '18';
  }
  
  return defaultVal;
};

The onPrompt function callback receives two parameters. The first parameter is the message to be displayed in the prompt dialog; this normally asks a question back to the user. The second parameter is the default value or answer.

Get Getting Started with PhantomJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.