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 ( ) ;
4
+ const browser = await puppeteer . launch ( {
5
+ headless : 'new' ,
6
+ } ) ;
6
7
const page = await browser . newPage ( ) ;
7
-
8
- // Navigate the page to a URL
8
+ await page . setViewport ( { width : 1280 , height : 800 } ) ;
9
9
await page . goto ( 'https://pptr.dev/' ) ;
10
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
11
+ await page . click ( 'button.DocSearch-Button' ) ;
12
+
13
+ await page . waitForSelector ( 'input.DocSearch-Input' ) ;
14
+ await page . click ( 'input.DocSearch-Input' ) ;
15
+ const keyword = 'andy popoo' ;
16
+ await page . keyboard . type ( keyword , { delay : 100 } ) ;
17
+
18
+ await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
19
+
20
+ const links = await page . evaluate ( ( ) => {
21
+ return Array . from ( document . querySelectorAll ( '.DocSearch-Hit a' ) ) . map ( a => a . textContent . trim ( ) ) ;
22
+ } ) ;
23
+
24
+ const targetIndex = links . findIndex ( text => text . includes ( 'ElementHandle.dragAndDrop() method' ) ) ;
25
+ await page . evaluate ( ( index ) => {
26
+ const allLinks = Array . from ( document . querySelectorAll ( '.DocSearch-Hit a' ) ) ;
27
+ allLinks [ index ] . click ( ) ;
28
+ } , targetIndex ) ;
29
+
30
+ await page . waitForSelector ( 'h1' ) ;
31
+ const title = await page . $eval ( 'h1' , el => el . textContent ) ;
32
+ console . log ( title ) ;
19
33
20
- // Close the browser
21
34
await browser . close ( ) ;
22
- } ) ( ) ;
35
+ } ) ( ) ;
0 commit comments