diff --git a/lab4/main_test.js b/lab4/main_test.js index e37d21a..0987234 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -1,22 +1,33 @@ const puppeteer = require('puppeteer'); (async () => { - // Launch the browser and open a new blank page - const browser = await puppeteer.launch(); - const page = await browser.newPage(); - - // 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 - - // Close the browser - await browser.close(); -})(); \ No newline at end of file + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + + await page.goto('https://pptr.dev/'); + await page.setViewport({ width: 1080, height: 1024 }); + + await page.click('button.DocSearch.DocSearch-Button'); + await page.waitForSelector('input#docsearch-input.DocSearch-Input'); + + await page.type('input#docsearch-input.DocSearch-Input', 'andy popoo'); + + await page.waitForSelector('.DocSearch-Hit'); + + const links = await page.$$('.DocSearch-Hit a'); + + for (const link of links) { + const text = await link.evaluate(el => el.textContent.trim()); + if (text === 'ElementHandle.dragAndDrop() method') { + const href = await link.evaluate(el => el.href); + await page.goto(href); + break; + } + } + + const titleElement = await page.waitForSelector('h1'); + const fullTitle = await titleElement.evaluate(el => el.textContent.trim()); + console.log(fullTitle); + + await browser.close(); +})();