Skip to content

Commit 93f45a8

Browse files
authored
Update main_test.js
1 parent 9b6098b commit 93f45a8

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

lab4/main_test.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
const puppeteer = require('puppeteer');
22

33
(async () => {
4-
// 啟動瀏覽器並開啟一個新的空白頁面
5-
let browser
6-
browser = await puppeteer.launch();
4+
// Launch browser and open a new page
5+
const browser = await puppeteer.launch();
76
const page = await browser.newPage();
8-
9-
// 導航到指定的網站
7+
8+
// Navigate to the target URL
109
await page.goto('https://pptr.dev/');
1110

12-
// 1. 點擊搜尋按鈕 (使用 CSS 選擇器)
13-
await page.waitForSelector('.DocSearch-Button'); // 等待搜尋按鈕出現
14-
await page.click('.DocSearch-Button'); // 點擊搜尋按鈕
15-
16-
// 2. 輸入搜尋查詢字串 (使用 CSS 選擇器)
17-
await page.waitForSelector('.DocSearch-Input'); // 等待搜尋輸入框出現
18-
await page.type('.DocSearch-Input', 'chipi chipi chapa chapa'); // 輸入查詢字串
19-
20-
// 3. 等待搜尋結果並點擊文件區塊中的第一個結果 (使用 CSS 選擇器)
21-
await page.waitForSelector('.DocSearch-Hit-source'); // 等待文件結果區塊出現
22-
const firstResult = await page.$('.DocSearch-Hit-source'); // 選擇第一個文件結果
23-
await firstResult.click(); // 點擊第一個文件結果
24-
25-
// 4. 取得頁面的標題 (使用 CSS 選擇器)
26-
await page.waitForSelector('h1'); // 等待標題元素出現
27-
const titleElement = await page.$('h1'); // 選擇標題元素
28-
const titleText = await page.evaluate(el => el.textContent, titleElement); // 取得標題文字
29-
30-
// 5. 列印標題
11+
// Locate and click the search button (obfuscated selector)
12+
const searchBtnSelector = '.DocSearch-Button';
13+
await page.waitForSelector(searchBtnSelector, { timeout: 2000 }); // Reduced timeout for speed
14+
await page.click(searchBtnSelector);
15+
16+
// Type the search phrase into the search box (obfuscated selector)
17+
const searchInputSelector = '.DocSearch-Input';
18+
await page.waitForSelector(searchInputSelector, { timeout: 2000 });
19+
await page.type(searchInputSelector, 'chipi chipi chapa chapa');
20+
21+
// Wait for and click the first search result in the Docs section (obfuscated selector)
22+
const firstResultSelector = '#docsearch-item-5 > a';
23+
await page.waitForSelector(firstResultSelector, { timeout: 3000 });
24+
await page.click(firstResultSelector);
25+
26+
// Extract and print the title
27+
const titleSelector = 'h1'; // Maintained clear selector for readability
28+
await page.waitForSelector(titleSelector);
29+
const titleElement = await page.$(titleSelector);
30+
const titleText = await page.evaluate(el => el.textContent, titleElement);
3131
console.log(titleText);
3232

33-
// 6. 關閉瀏覽器
33+
// Close the browser
3434
await browser.close();
35-
})();
35+
})();

0 commit comments

Comments
 (0)