1
1
const puppeteer = require ( 'puppeteer' ) ;
2
2
3
3
( async ( ) => {
4
- // Launch the browser and open a new blank page
5
- const browser = await puppeteer . launch ( ) ;
6
- const page = await browser . newPage ( ) ;
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
- } ) ( ) ;
4
+ const browser = await puppeteer . launch ( ) ;
5
+ const page = await browser . newPage ( ) ;
6
+
7
+ await page . goto ( 'https://pptr.dev/' ) ;
8
+ await page . setViewport ( { width : 1080 , height : 1024 } ) ;
9
+
10
+ await page . click ( 'button.DocSearch.DocSearch-Button' ) ;
11
+ await page . waitForSelector ( 'input#docsearch-input.DocSearch-Input' ) ;
12
+
13
+ await page . type ( 'input#docsearch-input.DocSearch-Input' , 'andy popoo' ) ;
14
+
15
+ await page . waitForSelector ( '.DocSearch-Hit' ) ;
16
+
17
+ const links = await page . $$ ( '.DocSearch-Hit a' ) ;
18
+
19
+ for ( const link of links ) {
20
+ const text = await link . evaluate ( el => el . textContent . trim ( ) ) ;
21
+ if ( text === 'ElementHandle.dragAndDrop() method' ) {
22
+ const href = await link . evaluate ( el => el . href ) ;
23
+ await page . goto ( href ) ;
24
+ break ;
25
+ }
26
+ }
27
+
28
+ const titleElement = await page . waitForSelector ( 'h1' ) ;
29
+ const fullTitle = await titleElement . evaluate ( el => el . textContent . trim ( ) ) ;
30
+ console . log ( fullTitle ) ;
31
+
32
+ await browser . close ( ) ;
33
+ } ) ( ) ;
0 commit comments