diff --git a/lab4/main_test.js b/lab4/main_test.js index e37d21a..04f33dc 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -1,22 +1,35 @@ const puppeteer = require('puppeteer'); (async () => { - // Launch the browser and open a new blank page - const browser = await puppeteer.launch(); + const browser = await puppeteer.launch({ + headless: 'new', + }); const page = await browser.newPage(); - - // Navigate the page to a URL + await page.setViewport({ width: 1280, height: 800 }); 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-Button'); + + await page.waitForSelector('input.DocSearch-Input'); + await page.click('input.DocSearch-Input'); + const keyword = 'andy popoo'; + await page.keyboard.type(keyword, { delay: 100 }); + + await new Promise(resolve => setTimeout(resolve, 2000)); + + const links = await page.evaluate(() => { + return Array.from(document.querySelectorAll('.DocSearch-Hit a')).map(a => a.textContent.trim()); + }); + + const targetIndex = links.findIndex(text => text.includes('ElementHandle.dragAndDrop() method')); + await page.evaluate((index) => { + const allLinks = Array.from(document.querySelectorAll('.DocSearch-Hit a')); + allLinks[index].click(); + }, targetIndex); + + await page.waitForSelector('h1'); + const title = await page.$eval('h1', el => el.textContent); + console.log(title); - // Close the browser await browser.close(); -})(); \ No newline at end of file +})();