@@ -2,21 +2,56 @@ const puppeteer = require('puppeteer');
22
33( async ( ) => {
44 // Launch the browser and open a new blank page
5- const browser = await puppeteer . launch ( ) ;
5+ const browser = await puppeteer . launch ( {
6+ headless : false ,
7+ slowMo : 1
8+ } ) ;
69 const page = await browser . newPage ( ) ;
7-
810 // Navigate the page to a URL
9- await page . goto ( 'https://pptr.dev/' ) ;
1011
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
12+ try {
13+ await page . goto ( 'https://pptr.dev/' ) ;
14+ await page . setViewport ( { width : 1080 , height : 1024 } ) ;
15+
16+ // Click search button
17+ await page . click ( '.DocSearch-Button' ) ;
18+ await page . waitForSelector ( '.DocSearch-Input' ) ;
19+ // Type into search box
20+ // Wait for search result
21+ await page . type ( '.DocSearch-Input' , 'Andy popoo' ) ;
22+
23+ // Get the `Docs` result section
24+ // Click on first result in `Docs` section
25+ await page . waitForFunction ( ( ) => {
26+ const bits = document . querySelectorAll ( '.DocSearch-Hit-source' ) ;
27+ return Array . from ( bits ) . some ( el =>
28+ el . textContent . trim ( ) === 'ElementHandle'
29+ ) ;
30+ } , { timeout : 8000 } ) ;
31+
32+ await page . evaluate ( ( ) => {
33+ const bits = document . querySelectorAll ( '.DocSearch-Hit-source' ) ;
34+ for ( const bite of bits ) {
35+ if ( bite . textContent . trim ( ) === 'ElementHandle' ) {
36+ const parent = bite . parentElement ;
37+ const first = parent . querySelector ( '.DocSearch-Hit a' ) ;
38+ if ( first ) {
39+ first . click ( ) ;
40+ return true ;
41+ }
42+ }
43+ }
44+ throw new Error ( 'No results found for elementhandle' ) ;
45+ } ) ;
46+
47+ // locate and print the title
48+ await page . waitForSelector ( 'h1' ) ;
49+ const header = await page . $eval ( 'h1' , el => el . textContent . trim ( ) ) ;
50+ console . log ( header ) ;
1951
20- // Close the browser
21- await browser . close ( ) ;
52+ } catch ( error ) {
53+ console . error ( 'Error:' , error . message ) ;
54+ } finally {
55+ await browser . close ( ) ;
56+ }
2257} ) ( ) ;
0 commit comments