Performing user events
In some cases, we will need to emulate user keyboard events and mouse events. PhantomJS provides a simple function to trigger these events from our script. These events will provide necessary actions in our script, from clicking on a URL to inputting usernames and passwords, or just filling up a form.
The function sendEvent
, from the WebPage API, has the capability to perform these events on the page from our scripts.
page.sendEvent(eventType[,event specific arguments])
There are two types of events that are supported: keyboard and mouse events. Each event differs by the set of arguments that can be passed to the functions to match the necessary data to perform the event.
Keyboard events
Let us first explore how to use key events. The following is the complete function signature for the key event.
page.sendEvent(keyEventType, keyOrKeys, null, null, modifier).
The key event type can be either 'keypress'
, 'keydown'
, or 'keyup'
. The second argument...