- PhantomJS Tutorial
- PhantomJS - Home
- PhantomJS - Overview
- PhantomJS - Environment Setup
- PhantomJS - Object
- PhantomJS - Methods
- WebPage Module
- PhantomJS - Properties
- PhantomJS - Methods
- PhantomJS - Events/Callbacks
- PhantomJS - Child Process Module
- File System Module
- PhantomJS - Properties
- PhantomJS - Methods
- System Module
- PhantomJS - Properties
- Web Server Module
- PhantomJS - Properties
- PhantomJS - Methods
- Miscellaneous
- Command Line Interface
- PhantomJS - Screen Capture
- PhantomJS - Page Automation
- PhantomJS - Network Monitoring
- PhantomJS - Testing
- PhantomJS - REPL
- PhantomJS - Examples
- PhantomJS Useful Resources
- PhantomJS - Quick Guide
- PhantomJS - Useful Resources
- PhantomJS - Discussion
PhantomJS - sendEvent()
It is used to send an event to the webpage. They are not DOM events. Each of these events are sent to the webpage based on the user interaction.
The events supported by this method are mouse and keyboard events.
Mouse Events
SendEvent (mouseEventType [, mouseX, mouseY, button = 'left'])
MouseEventType − This is a type of event and it supports mouseup, mousedown, mousemove, doubleclick and click.
The MouseX and MouseY events are optional and takes the mouse position. The button parameter defines the button to push. It is on the left by default. For mousemove, there is no button pressed, so button is not considered.
Keyboard Events
SendEvent (keyboardEventType, keyOrKeys, [null, null, modifier])
KeyboardEventType − This is a type of event and supports keyup, keypress and keydown.
Keyorkeys − Second parameter is the key from page.event.key or a string. The third and fourth are not considered and need to pass NULL for it.
Modifier − It is a integer and has the following list −
0 − No modifier key is pressed.
0x02000000 − A Shift key on the keyboard is pressed.
0x04000000 − A Ctrl key on the keyboard is pressed.
0x08000000 − An Alt key on the keyboard is pressed.
0x10000000 − A Meta key on the keyboard is pressed.
0x20000000 − A keypad button is pressed.
Syntax
Its syntax is as follows −
sendEvent(mouseEventType[, mouseX, mouseY, button = 'left'])
Example
Let us take an example to understand the use of sendEvent() method.
var page = require('webpage').create(); page.onAlert = function(msg) { console.log(msg); } page.open('http://localhost/tasks/click.html', function(status) { var element = page.evaluate(function() { return document.querySelector('.mybutton'); }); page.sendEvent('click', element.offsetLeft, element.offsetTop, 'left'); console.log('element is ' + element); });
click.html
<html> <body> <form> <input type = "button" class = "mybutton" value = "Click me" onclick = "clickme()"> </form> <p>welcome to phantomjs</p> <script> function clickme() { alert("Hello world!"); } </script> </body> </html>
The above program generates the following output.
Hello world! element is [object Object]
To Continue Learning Please Login
Login with Google