From a61f1a331771303f6c1c2ff6b5e64a9f4fb90d46 Mon Sep 17 00:00:00 2001 From: linn Date: Thu, 3 Apr 2025 05:16:16 +0800 Subject: [PATCH 1/2] hi --- lab4/main_test.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/lab4/main_test.js b/lab4/main_test.js index e37d21a..ef40d77 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -7,7 +7,72 @@ const puppeteer = require('puppeteer'); // Navigate the page to a URL await page.goto('https://pptr.dev/'); + // Click search button + await page.click('.DocSearch'); + + // Wait for search modal to appear + await page.waitForSelector('.DocSearch-Modal'); + + // Type into search box + await page.waitForSelector('.DocSearch-Input'); + await page.type('.DocSearch-Input', 'andy popoo'); + + // Wait for search results to appear + await page.waitForSelector('.DocSearch-Hit-source'); + + // 增加等待時間,確保所有結果都完全加載 + await new Promise(resolve => setTimeout(resolve, 1000)); + + // 使用page.evaluate直接在頁面上下文中找到ElementHandle部分並點擊第一個結果 + const clicked = await page.evaluate(() => { + + const sections = document.querySelectorAll('.DocSearch-Hit-source'); + + + // 尋找包含ElementHandle的部分 + for (let i = 0; i < sections.length; i++) { + if (sections[i].textContent.includes('ElementHandle')) { + console.log(`找到包含ElementHandle的部分: "${sections[i].textContent}"`); + + // 獲取這個部分的父元素 + const sectionParent = sections[i].parentElement; + + // 找到緊跟在這個部分後的第一個ul元素(這個ul包含搜索結果) + let nextElement = sections[i].nextElementSibling; + while (nextElement && nextElement.tagName !== 'UL') { + nextElement = nextElement.nextElementSibling; + } + + if (nextElement && nextElement.tagName === 'UL') { + // 找到ul中的第一個li元素(第一個搜索結果) + const firstLi = nextElement.querySelector('li'); + + if (firstLi) { + // 找到這個li中的a元素 + const link = firstLi.querySelector('a'); + + if (link) { + + link.click(); + return true; + } + } + } + + break; + } + } + + return false; + }); + + // Wait for page to load + await page.waitForSelector('h1'); + + // Get the title text + const title = await page.$eval('h1', el => el.textContent); + console.log(title); // Hints: // Click search button // Type into search box @@ -19,4 +84,4 @@ const puppeteer = require('puppeteer'); // Close the browser await browser.close(); -})(); \ No newline at end of file +})(); From ecb06083fb503d83613a3c11088b482f5fd7a119 Mon Sep 17 00:00:00 2001 From: linn Date: Thu, 3 Apr 2025 05:23:28 +0800 Subject: [PATCH 2/2] abc --- lab4/main_test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lab4/main_test.js b/lab4/main_test.js index ef40d77..2288403 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -32,7 +32,6 @@ const puppeteer = require('puppeteer'); // 尋找包含ElementHandle的部分 for (let i = 0; i < sections.length; i++) { if (sections[i].textContent.includes('ElementHandle')) { - console.log(`找到包含ElementHandle的部分: "${sections[i].textContent}"`); // 獲取這個部分的父元素 const sectionParent = sections[i].parentElement;