@@ -10,13 +10,46 @@ const puppeteer = require('puppeteer');
10
10
11
11
// Hints:
12
12
// Click search button
13
+ await page . waitForSelector ( '#__docusaurus > nav > div.navbar__inner > div.navbar__items.navbar__items--right > div.navbarSearchContainer_IP3a > button > span.DocSearch-Button-Container > svg' ) ;
14
+ await page . click ( '#__docusaurus > nav > div.navbar__inner > div.navbar__items.navbar__items--right > div.navbarSearchContainer_IP3a > button > span.DocSearch-Button-Container > svg' ) ;
13
15
// Type into search box
16
+ // await page.type('.navbarSearchContainer_IP3a input', 'chipi chipi chapa chapa');
17
+ await page . waitForSelector ( '.DocSearch-Input' ) ;
18
+ await page . type ( '.DocSearch-Input' , 'andy popoo' , { delay : 100 } ) ;
19
+
14
20
// Wait for search result
21
+ await page . waitForSelector ( '.DocSearch-Hit' ) ;
15
22
// Get the `Docs` result section
16
23
// Click on first result in `Docs` section
24
+ await page . waitForSelector ( '.DocSearch-Hit' , { timeout : 5000 } ) ;
25
+
26
+ const docsSection = await page . $$ ( '.DocSearch-Hit' ) ;
27
+ const limit = 5 ; // limit the number of results to show
28
+ const visibleDocsSection = docsSection . slice ( 0 , limit ) ; // get the first 5 results
29
+ if ( visibleDocsSection . length > 0 ) {
30
+ for ( let i = 0 ; i < visibleDocsSection . length ; i ++ ) {
31
+ const title = await visibleDocsSection [ i ] . $eval ( '.DocSearch-Hit-title' , el => el . innerText ) ;
32
+ // console.log(`搜尋結果 ${i + 1}: ${title}`);
33
+ }
34
+
35
+ // Click the first result
36
+ const clickedTitle = await visibleDocsSection [ 0 ] . $eval ( '.DocSearch-Hit-title' , el => el . innerText ) ;
37
+ // console.log(`即將點擊的搜尋結果: ${clickedTitle}`);
38
+
39
+ await visibleDocsSection [ 4 ] . click ( ) ;
40
+ } else {
41
+ console . log ( 'Results not found!' ) ;
42
+ }
43
+ // if (visibleDocsSection.length > 0) {
44
+ // await visibleDocsSection[0].click(); // 點擊第一個搜尋結果
45
+ // } else {
46
+ // console.log('搜尋結果沒有找到!');
47
+ // }
17
48
// Locate the title
18
49
// Print the title
19
-
50
+ await page . waitForSelector ( 'h1' ) ;
51
+ const title = await page . $eval ( 'h1' , element => element . innerText ) ;
52
+ console . log ( title ) ;
20
53
// Close the browser
21
54
await browser . close ( ) ;
22
55
} ) ( ) ;
0 commit comments