Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
REMitchell
GitHub Repository: REMitchell/python-crawling
Path: blob/master/6-Selenium/phantomjs/examples/page_events.js
164 views
1
// The purpose of this is to show how and when events fire, considering 5 steps
2
// happening as follows:
3
//
4
// 1. Load URL
5
// 2. Load same URL, but adding an internal FRAGMENT to it
6
// 3. Click on an internal Link, that points to another internal FRAGMENT
7
// 4. Click on an external Link, that will send the page somewhere else
8
// 5. Close page
9
//
10
// Take particular care when going through the output, to understand when
11
// things happen (and in which order). Particularly, notice what DOESN'T
12
// happen during step 3.
13
//
14
// If invoked with "-v" it will print out the Page Resources as they are
15
// Requested and Received.
16
//
17
// NOTE.1: The "onConsoleMessage/onAlert/onPrompt/onConfirm" events are
18
// registered but not used here. This is left for you to have fun with.
19
// NOTE.2: This script is not here to teach you ANY JavaScript. It's aweful!
20
// NOTE.3: Main audience for this are people new to PhantomJS.
21
22
var sys = require("system"),
23
page = require("webpage").create(),
24
logResources = false,
25
step1url = "http://en.wikipedia.org/wiki/DOM_events",
26
step2url = "http://en.wikipedia.org/wiki/DOM_events#Event_flow";
27
28
if (sys.args.length > 1 && sys.args[1] === "-v") {
29
logResources = true;
30
}
31
32
function printArgs() {
33
var i, ilen;
34
for (i = 0, ilen = arguments.length; i < ilen; ++i) {
35
console.log(" arguments[" + i + "] = " + JSON.stringify(arguments[i]));
36
}
37
console.log("");
38
}
39
40
////////////////////////////////////////////////////////////////////////////////
41
42
page.onInitialized = function() {
43
console.log("page.onInitialized");
44
printArgs.apply(this, arguments);
45
};
46
page.onLoadStarted = function() {
47
console.log("page.onLoadStarted");
48
printArgs.apply(this, arguments);
49
};
50
page.onLoadFinished = function() {
51
console.log("page.onLoadFinished");
52
printArgs.apply(this, arguments);
53
};
54
page.onUrlChanged = function() {
55
console.log("page.onUrlChanged");
56
printArgs.apply(this, arguments);
57
};
58
page.onNavigationRequested = function() {
59
console.log("page.onNavigationRequested");
60
printArgs.apply(this, arguments);
61
};
62
page.onRepaintRequested = function() {
63
console.log("page.onRepaintRequested");
64
printArgs.apply(this, arguments);
65
};
66
67
if (logResources === true) {
68
page.onResourceRequested = function() {
69
console.log("page.onResourceRequested");
70
printArgs.apply(this, arguments);
71
};
72
page.onResourceReceived = function() {
73
console.log("page.onResourceReceived");
74
printArgs.apply(this, arguments);
75
};
76
}
77
78
page.onClosing = function() {
79
console.log("page.onClosing");
80
printArgs.apply(this, arguments);
81
};
82
83
// window.console.log(msg);
84
page.onConsoleMessage = function() {
85
console.log("page.onConsoleMessage");
86
printArgs.apply(this, arguments);
87
};
88
89
// window.alert(msg);
90
page.onAlert = function() {
91
console.log("page.onAlert");
92
printArgs.apply(this, arguments);
93
};
94
// var confirmed = window.confirm(msg);
95
page.onConfirm = function() {
96
console.log("page.onConfirm");
97
printArgs.apply(this, arguments);
98
};
99
// var user_value = window.prompt(msg, default_value);
100
page.onPrompt = function() {
101
console.log("page.onPrompt");
102
printArgs.apply(this, arguments);
103
};
104
105
////////////////////////////////////////////////////////////////////////////////
106
107
setTimeout(function() {
108
console.log("");
109
console.log("### STEP 1: Load '" + step1url + "'");
110
page.open(step1url);
111
}, 0);
112
113
setTimeout(function() {
114
console.log("");
115
console.log("### STEP 2: Load '" + step2url + "' (load same URL plus FRAGMENT)");
116
page.open(step2url);
117
}, 5000);
118
119
setTimeout(function() {
120
console.log("");
121
console.log("### STEP 3: Click on page internal link (aka FRAGMENT)");
122
page.evaluate(function() {
123
var ev = document.createEvent("MouseEvents");
124
ev.initEvent("click", true, true);
125
document.querySelector("a[href='#Event_object']").dispatchEvent(ev);
126
});
127
}, 10000);
128
129
setTimeout(function() {
130
console.log("");
131
console.log("### STEP 4: Click on page external link");
132
page.evaluate(function() {
133
var ev = document.createEvent("MouseEvents");
134
ev.initEvent("click", true, true);
135
document.querySelector("a[title='JavaScript']").dispatchEvent(ev);
136
});
137
}, 15000);
138
139
setTimeout(function() {
140
console.log("");
141
console.log("### STEP 5: Close page and shutdown (with a delay)");
142
page.close();
143
setTimeout(function(){
144
phantom.exit();
145
}, 100);
146
}, 20000);
147
148