From 5dd826997d70b7bfb017b78a129397c800d004dc Mon Sep 17 00:00:00 2001 From: Ramez Date: Wed, 2 Apr 2025 17:13:18 +0800 Subject: [PATCH 1/3] Script to find the title of API --- lab4/main_test.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lab4/main_test.js b/lab4/main_test.js index e37d21a..a28d7ad 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -1,22 +1,36 @@ const puppeteer = require('puppeteer'); - +const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); (async () => { // Launch the browser and open a new blank page - const browser = await puppeteer.launch(); + const browser = await puppeteer.launch({ headless: false }); const page = await browser.newPage(); // Navigate the page to a URL await page.goto('https://pptr.dev/'); + await new Promise(resolve => setTimeout(resolve, 1000)); // wait 5 seconds - // Hints: // Click search button + await page.click('button.DocSearch'); + await page.waitForSelector('input.DocSearch-Input'); + // Type into search box + await page.type('input.DocSearch-Input', 'andy popoo'); + // Wait for search result - // Get the `Docs` result section - // Click on first result in `Docs` section + await new Promise(resolve => setTimeout(resolve, 1000)); + + // Get the `ElementHandle` result section + // Click on first result in `ElementHandle` section + await page.click('#docsearch-hits1-item-4'); + // Locate the title // Print the title - + const title = await page.evaluate(() => { + return document.querySelector("div.theme-doc-markdown > header > h1").innerText; + }); + console.log(title); + // Close the browser await browser.close(); -})(); \ No newline at end of file +})(); + From 290f0d1ff60d19adbd23fdbb34abdcb72b2b4533 Mon Sep 17 00:00:00 2001 From: Ramez Date: Wed, 2 Apr 2025 19:35:34 +0800 Subject: [PATCH 2/3] Reworked script --- lab4/main_test.js | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/lab4/main_test.js b/lab4/main_test.js index a28d7ad..564c4ea 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -7,30 +7,40 @@ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); // Navigate the page to a URL await page.goto('https://pptr.dev/'); - await new Promise(resolve => setTimeout(resolve, 1000)); // wait 5 seconds - // Click search button await page.click('button.DocSearch'); await page.waitForSelector('input.DocSearch-Input'); + await page.type('input.DocSearch-Input', 'andy popoo'); + await new Promise(resolve => setTimeout(resolve, 1000)); + await page.waitForSelector('section.DocSearch-Hits'); - // Type into search box - await page.type('input.DocSearch-Input', 'andy popoo'); + const sections = await page.$$('section.DocSearch-Hits'); - // Wait for search result - await new Promise(resolve => setTimeout(resolve, 1000)); + for (const section of sections) { + const sourceDiv = await section.$('div.DocSearch-Hit-source'); + + if (sourceDiv) { + const text = await sourceDiv.evaluate(el => el.innerText.trim()); + if (text === 'ElementHandle') { + // Click on the first result in this section + const firstListItem = await section.$('#docsearch-list li'); + if (firstListItem) { + await firstListItem.click(); + await new Promise(resolve => setTimeout(resolve, 1000)); + const title = await page.evaluate(() => { + return document.querySelector("div.theme-doc-markdown > header > h1").innerText; + }); + console.log(title); + } else { + console.log('No list item found in this section'); + } + } + + } else { + console.log('No ElementHandle found in this section'); + } + } - // Get the `ElementHandle` result section - // Click on first result in `ElementHandle` section - await page.click('#docsearch-hits1-item-4'); - - // Locate the title - // Print the title - const title = await page.evaluate(() => { - return document.querySelector("div.theme-doc-markdown > header > h1").innerText; - }); - console.log(title); - - // Close the browser await browser.close(); })(); From 58a1248b4342b2373e8e0b3d7ff0d8c7bee85320 Mon Sep 17 00:00:00 2001 From: Ramez Date: Wed, 2 Apr 2025 19:53:27 +0800 Subject: [PATCH 3/3] Reworked script --- lab4/main_test.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lab4/main_test.js b/lab4/main_test.js index 564c4ea..8765a19 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -1,8 +1,7 @@ const puppeteer = require('puppeteer'); -const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); (async () => { // Launch the browser and open a new blank page - const browser = await puppeteer.launch({ headless: false }); + const browser = await puppeteer.launch(); const page = await browser.newPage(); // Navigate the page to a URL @@ -12,7 +11,6 @@ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); await page.waitForSelector('input.DocSearch-Input'); await page.type('input.DocSearch-Input', 'andy popoo'); await new Promise(resolve => setTimeout(resolve, 1000)); - await page.waitForSelector('section.DocSearch-Hits'); const sections = await page.$$('section.DocSearch-Hits');