From 1cbb3d81dbc7f68f40807f6672a6ff1464b503fe Mon Sep 17 00:00:00 2001 From: vivian921207 <67675816+vivian921207@users.noreply.github.com> Date: Wed, 2 Apr 2025 02:42:51 +0800 Subject: [PATCH 1/2] Update main_test.js --- lab4/main_test.js | 61 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/lab4/main_test.js b/lab4/main_test.js index e37d21a..657258c 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -1,22 +1,51 @@ const puppeteer = require('puppeteer'); (async () => { - // Launch the browser and open a new blank page - const browser = await puppeteer.launch(); - const page = await browser.newPage(); + const browser = await puppeteer.launch({ + headless: true, // ← 顯示瀏覽器畫面 + slowMo: 100 // ← 每步操作加 100ms 延遲,方便觀察 + }); + - // Navigate the page to a URL - await page.goto('https://pptr.dev/'); + const page = await browser.newPage(); + await page.goto('https://pptr.dev/'); + await page.setViewport({ width: 1280, height: 800 }); - // Hints: - // Click search button - // Type into search box - // Wait for search result - // Get the `Docs` result section - // Click on first result in `Docs` section - // Locate the title - // Print the title + await page.waitForSelector('button.DocSearch-Button'); + await page.click('button.DocSearch-Button'); - // Close the browser - await browser.close(); -})(); \ No newline at end of file + await page.waitForSelector('input.DocSearch-Input'); + await page.type('input.DocSearch-Input', 'andy popoo'); + + const sections = await page.$$('section.DocSearch-Hits'); + let clicked = false; + + for (const section of sections) { + // 抓出分類名稱 + const label = await section.$('.DocSearch-Hit-source'); + const labelText = label + ? await label.evaluate(el => el.textContent.trim()) + : ''; + + // 如果是 Documentation 區塊 + if (labelText === 'ElementHandle') { + const firstLink = await section.$('li.DocSearch-Hit a'); + if (firstLink) { + await firstLink.click(); + + // 給點時間等待跳轉或內容更新(因為可能是錨點 #) + await new Promise(resolve => setTimeout(resolve, 1000)); + + const title = await page.$eval('h1', el => el.textContent.trim()); + console.log(title); + + + clicked = true; + break; + } else { + console.warn("⚠️ Documentation 區有資料,但沒有可點擊連結"); + } + } + } + await browser.close(); +})(); From 624929b2eab09d464d00ac6c2adcdb9460ec7a76 Mon Sep 17 00:00:00 2001 From: vivian921207 <67675816+vivian921207@users.noreply.github.com> Date: Thu, 3 Apr 2025 12:47:55 +0800 Subject: [PATCH 2/2] Update main_test.js --- lab4/main_test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lab4/main_test.js b/lab4/main_test.js index e3e3307..657258c 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -49,4 +49,3 @@ const puppeteer = require('puppeteer'); } await browser.close(); })(); -