1
+ //把待格式化代碼粘貼到這裡
2
+
1
3
const puppeteer = require ( 'puppeteer' ) ;
2
4
3
5
( async ( ) => {
4
- // Launch the browser and open a new blank page
5
- const browser = await puppeteer . launch ( ) ;
6
- const page = await browser . newPage ( ) ;
6
+ const browser = await puppeteer . launch ( {
7
+ headless : false
8
+ } ) ; // 打開chrome
9
+ const page = await browser . newPage ( ) ; // 打開一個分頁
7
10
8
- // Navigate the page to a URL
9
11
await page . goto ( 'https://pptr.dev/' ) ;
10
12
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
13
+ // 等待搜索框父元素出现
14
+ await page . waitForSelector ( '.navbarSearchContainer_Bca1' ) ;
15
+
16
+ // 在搜索框父元素下查找按钮元素
17
+ // 1. Find the button element in the search bar parent element
18
+ console . log ( 'Step 1: Finding the button element' ) ;
19
+ const button = await page . $ ( '.navbarSearchContainer_Bca1 button.DocSearch' ) ;
20
+ if ( button ) {
21
+ console . log ( 'Step 1: Button element found' ) ;
22
+ } else {
23
+ console . error ( 'Step 1: Button element not found' ) ;
24
+ }
25
+ // 点击按钮
26
+ console . log ( 'Step 2: Clicking the button' ) ;
27
+ await button . click ( ) ;
28
+
29
+ // 等待搜索框内部的元素出现
30
+ console . log ( 'Step 3: Waiting for the element inside the search bar' ) ;
31
+ await page . waitForSelector ( '.DocSearch-Input' ) ;
32
+ console . log ( 'Step 3: Element inside the search bar found' ) ;
33
+
34
+ // 在搜索框内部查找输入框
35
+ console . log ( 'Step 4: Finding the input box inside the search bar' ) ;
36
+ const input = await page . $ ( '.DocSearch-Input' ) ;
37
+ if ( input ) {
38
+ console . log ( 'Step 4: Input box found' ) ;
39
+ } else {
40
+ console . error ( 'Step 4: Input box not found' ) ;
41
+ }
42
+
43
+ // 在搜索框内部输入关键词
44
+ console . log ( 'Step 5: Entering the keyword in the search bar' ) ;
45
+ await input . type ( 'chipi chipi chapa chapa' ) ;
46
+ console . log ( 'Step 5: Keyword entered' ) ;
47
+
48
+ // scroll
49
+ console . log ( 'Step 6: Scrolling to the search results list' ) ;
50
+ const element = await page . $ ( '#docsearch-list' ) ;
51
+ await element . scrollIntoView ( '#docsearch-list' ) ;
52
+ console . log ( 'Step 6: Scrolled to the search results list' ) ;
53
+
54
+ console . log ( 'Step 7: Clicking the target element (docsearch-item-5)' ) ;
55
+ async function clickTargetElement ( ) {
56
+ try {
57
+ // 使用 CSS 选择器定位目标元素
58
+ const targetElement = await page . $ ( '#docsearch-item-5' ) ;
59
+
60
+ // 点击目标元素
61
+ await targetElement . click ( ) ;
62
+ console . log ( '已点击目标元素' ) ;
63
+ } catch ( error ) {
64
+ console . error ( '点击元素错误:' , error ) ;
65
+ }
66
+ }
67
+
68
+ const searchResultSelector = '.devsite-result-item-link' ;
69
+
70
+ console . log ( 'Step 8: Waiting for the search results box to appear' ) ;
71
+ const searchResultsBox = await page . waitForSelector ( '#docsearch-item-5' ) ;
72
+ if ( searchResultsBox ) {
73
+ console . log ( 'Step 8: Search results box found' ) ;
74
+ } else {
75
+ console . error ( 'Step 8: Search results box not found' ) ;
76
+ }
77
+ // 等待結果框
78
+
79
+ // Wait for 2 seconds before clicking the result item title
80
+ console . log ( 'Step 9: Waiting for 2 seconds' ) ;
81
+ await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
82
+ console . log ( 'Step 9: 2 seconds passed' ) ;
83
+ /*
84
+ //单击第一个结果项标题可使用 id 属性或 DocSearch-Hit-title 或 DocSearch-Hit-path 类,定位标题或路径元素DocSearch-Hit-title 或 DocSearch-Hit-path
85
+ const firstResultTitle = await page.evaluate(() => {
86
+ return element.querySelector('#docsearch-item-5 .DocSearch-Hits:nth-child(2)');
87
+ });
88
+ */
89
+
90
+ console . log ( 'Step 10: Clicking the search results box' ) ;
91
+ await searchResultsBox . click ( ) ;
92
+ const isVisible = await searchResultsBox . isVisible ( ) ;
93
+ if ( isVisible ) {
94
+ console . log ( 'SearchResultsBox 元素可见' ) ;
95
+ } else {
96
+ console . log ( 'SearchResultsBox 元素不可见' ) ;
97
+ }
98
+ console . log ( 'Step 10: Search results box clicked' ) ;
99
+ /*
100
+ //Replace with actual ID or class
101
+ const titleElement = await page.waitForSelector('#docsearch-item-5 .DocSearch-Hits:nth-child(2)');
102
+
103
+ await titleElement.click();
104
+ */
105
+ // Get the page title
106
+ console . log ( 'Step 11: Getting the page title' ) ;
107
+ const title = await page . title ( ) ;
108
+ console . log ( 'Step 11: Page title:' , title ) ;
109
+
110
+ // Print the title to the console
111
+ console . log ( 'Step 12: Printing the title to the console' ) ;
112
+ console . log ( title ) ;
19
113
20
- // Close the browser
21
114
await browser . close ( ) ;
22
- } ) ( ) ;
115
+ } ) ( ) ;
0 commit comments