-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_simula-click.js
63 lines (47 loc) · 1.54 KB
/
08_simula-click.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Objeto da API Webpage
var webpage = require('webpage');
var page = webpage.create();
//Objeto da API System
var system = require('system');
url = system.args[1];
//Definição da resolução que a página será acessada
page.viewportSize = {width: 1366, height: 768};
//Dimensões do recorte da tela para fins de captura
page.clipRect = { top: 0, left: 0, width: 400, height: 400 };
if (url !== undefined ){
page.open(url, function(status){
console.log(status);
if (status == "success"){
//Avalia a página no contexto dela
var point = page.evaluate(function () {
//Captura o elemento
var elemento = document.getElementById('botao');
//Retorna o DOMRect do elemento selecionado
var rect = elemento.getBoundingClientRect();
//Posiciona no centro do elemento
return {
x: rect.left + Math.floor(rect.width / 2),
y: rect.top + (rect.height / 2)
};
});
//Executa o click na posição
page.sendEvent('click',point.x, point.y);
//Salva a imagem da página carregada
page.render("captura.jpg");
}
phantom.exit(0);
});
page.onError = function(msg, trace) {
var msgStack = ['Erro: ' + msg];
if (trace && trace.length) {
msgStack.push('Local do erro:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
});
}
console.error(msgStack.join('\n'));
};
} else {
console.log("Uso: phantomjs 08_simula-click.js <URL>");
phantom.exit(0);
}