1
1
# Scraping and verification
2
2
3
- Playwright allows verifiying state of the page and catching abnormal behavior by:
4
- - evaluating JavaScript code snippets in the page
5
- - capturing screenshots (png or jpeg)
6
- - listening console messages
7
- - observing uncaghut exceptions in the page
8
- - observing page crashes
9
- - etc
10
-
11
3
#### Contents
12
4
- [ Evaluating JavaScript] ( #evaluating-javascript )
13
5
- [ Capturing screenshot] ( #capturing-screenshot )
14
- - [ Listening console messages] ( #listening-console-messages )
15
- - [ Uncaghut exceptions] ( #uncaghut-exceptions )
16
- - [ Page crashes] ( #page-crashes )
6
+ - [ Page events] ( #page-events )
7
+ - [ Handling exceptions] ( #handling-exceptions )
17
8
18
9
<br />
19
10
20
11
## Evaluating JavaScript
21
12
22
13
Execute JavaScript function in the page:
23
14
``` js
24
- const href = await page .evaluate (() => document .location .href );
15
+ const href = await page .evaluate (() => document .location .href );
25
16
```
26
17
27
- If the result is a Promise or if the function is asynchronouse eveluate will automatically wait until it's resolved:
18
+ If the result is a Promise or if the function is asynchronous evaluate will automatically wait until it's resolved:
28
19
``` js
29
- const status = await page .evaluate (async () => {
30
- const response = await fetch (location .href );
31
- return response .status ;
32
- });
20
+ const status = await page .evaluate (async () => {
21
+ const response = await fetch (location .href );
22
+ return response .status ;
23
+ });
33
24
```
34
25
35
26
Get object handle and use it in multiple evaluations:
36
27
``` js
37
- // Create a new array in the page, wriate a reference to it in
38
- // window.myArray and get a handle to it.
39
- const myArrayHandle = await page .evaluateHandle (() => {
40
- window .myArray = [1 ];
41
- return myArray;
42
- });
43
-
44
- // Get current length of the array using the handle.
45
- const length = await page .evaluate (
46
- (arg ) => arg .myArray .length ,
47
- { myArray: myArrayHandle }
48
- );
49
-
50
- // Add one more element to the array using the handle
51
- await page .evaluate ((arg ) => arg .myArray .push (arg .newElement ), {
52
- myArray: myArrayHandle,
53
- newElement: 2
54
- });
55
-
56
- // Get current length of the array using window.myArray reference.
57
- const newLength = await page .evaluate (() => window .myArray .length );
58
-
59
- // Release the object when it's no longer needed.
60
- await myArrayHandle .dispose ();
28
+ // Create a new array in the page, write a reference to it in
29
+ // window.myArray and get a handle to it.
30
+ const myArrayHandle = await page .evaluateHandle (() => {
31
+ window .myArray = [1 ];
32
+ return myArray;
33
+ });
34
+
35
+ // Get current length of the array using the handle.
36
+ const length = await page .evaluate (
37
+ (arg ) => arg .myArray .length ,
38
+ { myArray: myArrayHandle }
39
+ );
40
+
41
+ // Add one more element to the array using the handle
42
+ await page .evaluate ((arg ) => arg .myArray .push (arg .newElement ), {
43
+ myArray: myArrayHandle,
44
+ newElement: 2
45
+ });
46
+
47
+ // Get current length of the array using window.myArray reference.
48
+ const newLength = await page .evaluate (() => window .myArray .length );
49
+
50
+ // Release the object when it's no longer needed.
51
+ await myArrayHandle .dispose ();
61
52
```
62
53
63
54
#### API reference
@@ -75,90 +66,95 @@ Get object handle and use it in multiple evaluations:
75
66
76
67
Take screenshot of the page's viewport and save it in a png file:
77
68
``` js
78
- await page .screenshot ({path: ' screenshot.png' });
69
+ await page .screenshot ({path: ' screenshot.png' });
79
70
```
80
- Capture entire scrollable area of the page:
71
+
72
+ #### Variations
73
+
74
+ Capture particular element:
81
75
``` js
82
- await page .screenshot ({path: ' screenshot.png' , fullPage: true });
76
+ const elementHandle = await page .$ (' .header' );
77
+ await elementHandle .screenshot ({ path: ' screenshot.png' });
83
78
```
84
- Save screenshot in an in-memory buffer (the content is base64-encoded image bytes):
79
+
80
+ Capture full page screenshot:
81
+ ``` js
82
+ await page .screenshot ({path: ' screenshot.png' , fullPage: true });
83
+ ```
84
+
85
+ Capture screenshot into a Node [ Buffer] ( https://nodejs.org/api/buffer.html#buffer_class_buffer ) .
85
86
``` js
86
- const buffer = await page .screenshot ();
87
- // Log the length.
88
- console .log (buffer .length );
87
+ const buffer = await page .screenshot ();
88
+ // Log the length.
89
+ console .log (buffer .toString ( ' base64 ' ) );
89
90
```
90
91
91
92
92
93
#### API reference
93
94
94
95
- [ page.screenshot([ options] )] ( ./api.md#pagescreenshotoptions )
95
- - [ Node.js Buffer ] ( https://nodejs.org /api/buffer.html )
96
+ - [ elementHandle.screenshot( [ options ] ) ] ( . /api.md#elementhandlescreenshotoptions )
96
97
97
98
<br />
98
99
99
- ## Listening console messages
100
+ ## Page events
100
101
101
- Listen all console messages in a page and dump _ errors_ in to the terminal:
102
+ You can listen for various events on the ` page ` object. Following are just some of the examples of the events you can assert and handle:
103
+
104
+ #### "` console ` " - get all console messages from the page
102
105
103
106
``` js
104
- // Get all console messages from the page.
105
- page .on (' console' , msg => {
106
- // Handle only errors.
107
- if (msg .type () !== ' error' )
108
- return ;
109
- console .log (` text: "${ msg .text ()} "` );
110
- });
111
-
112
- await page .evaluate (() => console .error (' Page error message' ));
113
- ```
114
- Get access to the console message arguments:
115
- ``` js
116
- page .on (' console' , msg => {
117
- for (let i = 0 ; i < msg .args ().length ; ++ i)
118
- console .log (` ${ i} : ${ msg .args ()[i]} ` );
119
- });
107
+ page .on (' console' , msg => {
108
+ // Handle only errors.
109
+ if (msg .type () !== ' error' )
110
+ return ;
111
+ console .log (` text: "${ msg .text ()} "` );
112
+ });
120
113
```
121
114
122
- #### API reference
123
-
124
- - [ class: ConsoleMessage] ( ./api.md#class-consolemessage )
125
- - [ class ` JSHandle ` ] ( ./api.md#class-jshandle )
126
- - [ event: 'console'] ( ./api.md#event-console )
127
-
128
- <br />
115
+ #### "` dialog ` " - Handle assert, confirm, prompt
129
116
117
+ ``` js
118
+ page .on (' dialog' , dialog => {
119
+ dialog .accept ();
120
+ });
121
+ ```
130
122
131
- ## Uncaghut exceptions
123
+ #### " ` popup ` " - Handle assert, confirm, prompt
132
124
133
- Listen uncaghut exceptions in the page:
134
125
``` js
135
- // Log all uncaught errors to the terminal
136
- page .on (' pageerror' , exception => {
137
- console .log (` Uncaught exception: "${ exception} "` );
138
- });
139
-
140
- // Navigate to a page with an exception.
141
- await page .goto (' data:text/html,<script>throw new Error("Test")</script>' );
126
+ const [popup ] = await Promise .all ([
127
+ page .waitForEvent (' popup' ),
128
+ page .click (' #open' )
129
+ ]);
142
130
```
143
131
144
132
#### API reference
145
133
146
- - [ event: 'pageerror'] ( ./api.md#event-pageerror )
134
+ - [ class: Page] ( ./api.md#class-page )
135
+ - [ event: 'console'] ( ./api.md#event-console )
136
+ - [ event: 'dialog'] ( ./api.md#event-dialog )
137
+ - [ event: 'popup'] ( ./api.md#event-popup )
138
+ - [ class: ConsoleMessage] ( ./api.md#class-consolemessage )
147
139
148
140
<br />
149
141
150
- ## Page crashes
151
142
152
- Listen to page crashes:
143
+ ## Handling exceptions
144
+
145
+ Listen uncaught exceptions in the page:
153
146
``` js
154
- page .on (' crash' , exception => {
155
- console .log (` Page crashed` );
156
- });
147
+ // Log all uncaught errors to the terminal
148
+ page .on (' pageerror' , exception => {
149
+ console .log (` Uncaught exception: "${ exception} "` );
150
+ });
151
+
152
+ // Navigate to a page with an exception.
153
+ await page .goto (' data:text/html,<script>throw new Error("Test")</script>' );
157
154
```
158
- It's very unusual for page to crash but might happen if a page allocates too much memory or due to a bug in a browser.
159
155
160
156
#### API reference
161
157
162
- - [ event: 'crash '] ( ./api.md#event-crash )
158
+ - [ event: 'pageerror '] ( ./api.md#event-pageerror )
163
159
164
160
<br />
0 commit comments