它用於向網頁發送事件。它們不是 DOM 事件。這些事件中的每一個都根據用戶交互發送到網頁。
此方法支持的事件是鼠標和鍵盤事件。
鼠標事件
SendEvent (mouseEventType [, mouseX, mouseY, button = 'left'])
MouseEventType- 這是一種事件,它支持mouseup, mousedown, mousemove, doubleclick和click。
這個MouseX和MouseY事件是可選的,並采用鼠標位置。 button 參數定義要按下的按鈕。默認情況下它在左側。對於 mousemove,沒有按下按鈕,因此不考慮按鈕。
鍵盤事件
SendEvent (keyboardEventType, keyOrKeys, [null, null, modifier])
KeyboardEventType- 這是一種事件並支持keyup, keypress和keydown.
Keyorkeys− 第二個參數是 page.event.key 中的鍵或字符串。第三個和第四個不考慮,需要為它傳遞NULL。
Modifier- 它是一個整數,具有以下列表 -
0− 未按下修飾鍵。
0x02000000− 按下鍵盤上的 Shift 鍵。
0x04000000− 按下鍵盤上的 Ctrl 鍵。
0x08000000− 按下鍵盤上的 Alt 鍵。
0x10000000− 按下鍵盤上的 Meta 鍵。
0x20000000− 按下了鍵盤按鈕。
用法
它的語法如下 -
sendEvent(mouseEventType[, mouseX, mouseY, button = 'left'])
示例
讓我們舉個例子來理解使用sendEvent()方法。
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>
上述程序生成以下內容output。
Hello world! element is [object Object]
相關用法
- PhantomJS setContent()用法及代碼示例
- PhantomJS switchToFocusedFrame()用法及代碼示例
- PhantomJS stop()用法及代碼示例
- PhantomJS onConsoleMessage()用法及代碼示例
- PhantomJS onResourceRequested()用法及代碼示例
- PhantomJS onPageCreated()用法及代碼示例
- PhantomJS deleteCookie()用法及代碼示例
- PhantomJS renderBase64()用法及代碼示例
- PhantomJS onUrlChanged()用法及代碼示例
- PhantomJS onNavigationRequested()用法及代碼示例
- PhantomJS onAlert()用法及代碼示例
注:本文由純淨天空篩選整理自 PhantomJS - sendEvent()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。