@@ -5,18 +5,47 @@ const puppeteer = require('puppeteer');
5
5
const browser = await puppeteer . launch ( ) ;
6
6
const page = await browser . newPage ( ) ;
7
7
8
- // Navigate the page to a URL
9
- await page . goto ( 'https://pptr.dev/' ) ;
10
-
11
- // Hints:
12
- // Click search button
13
- // Type into search box
14
- // Wait for search result
15
- // Get the `Docs` result section
16
- // Click on first result in `Docs` section
17
- // Locate the title
18
- // Print the title
19
-
20
- // Close the browser
21
- await browser . close ( ) ;
22
- } ) ( ) ;
8
+ try {
9
+ // Navigate the page to a URL
10
+ await page . goto ( 'https://pptr.dev/' ) ;
11
+
12
+ // Hints:
13
+ // Click search button
14
+ // Type into search box
15
+ // Wait for search result
16
+ // Get the `Docs` result section
17
+ // Click on first result in `Docs` section
18
+ // Locate the title
19
+ // Print the title
20
+
21
+ // Click search button
22
+ await page . click ( 'button.DocSearch.DocSearch-Button' ) ;
23
+
24
+ // Type into search box
25
+ await page . waitForSelector ( '#docsearch-input' ) ;
26
+ await page . type ( '#docsearch-input' , 'chipi chipi chapa chapa' , { delay : 1000 } ) ;
27
+
28
+ // Wait for search result
29
+ await page . waitForSelector ( '#docsearch-item-5' ) ;
30
+
31
+ // Click on first result in `Docs` section
32
+ await page . click ( '#docsearch-item-5' ) ;
33
+
34
+ // Locate the title
35
+ await page . waitForSelector ( 'h1' ) ;
36
+
37
+ // Get the text content of the title element
38
+ const title = await page . evaluate ( ( ) => {
39
+ const titleElement = document . querySelector ( 'h1' ) ;
40
+ return titleElement . textContent ;
41
+ } ) ;
42
+
43
+ // Print the title
44
+ console . log ( title ) ;
45
+ } catch ( error ) {
46
+ console . error ( 'An error occurred:' , error ) ;
47
+ } finally {
48
+ // Close the browser
49
+ await browser . close ( ) ;
50
+ }
51
+ } ) ( ) ;
0 commit comments