diff --git a/lab4/main_test.js b/lab4/main_test.js index e37d21a..8765a19 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -1,5 +1,4 @@ const puppeteer = require('puppeteer'); - (async () => { // Launch the browser and open a new blank page const browser = await puppeteer.launch(); @@ -8,15 +7,38 @@ const puppeteer = require('puppeteer'); // Navigate the page to a URL await page.goto('https://pptr.dev/'); - // 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.click('button.DocSearch'); + await page.waitForSelector('input.DocSearch-Input'); + await page.type('input.DocSearch-Input', 'andy popoo'); + await new Promise(resolve => setTimeout(resolve, 1000)); + + const sections = await page.$$('section.DocSearch-Hits'); - // Close the browser + 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'); + } + } + await browser.close(); -})(); \ No newline at end of file +})(); +