Skip to content

[LAB4] 313552209 #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: 313552009
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 48 additions & 12 deletions lab4/main_test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
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:false});
const page = await browser.newPage();

// Navigate the page to a URL
// Set viewport size
await page.setViewport({ width: 1080, height: 1024 });

// Navigate to the Puppeteer official website
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
// Click the search button in the top-right navbar
const searchBtnSelector =
'#__docusaurus > nav > div.navbar__inner > div.navbar__items.navbar__items--right > div.navbarSearchContainer_IP3a > button > span.DocSearch-Button-Container > svg';
await page.waitForSelector(searchBtnSelector);
await page.click(searchBtnSelector);

// Type search query into the search input
const searchInputSelector = '#docsearch-input';
await page.waitForSelector(searchInputSelector);
await page.type(searchInputSelector, 'andy popoo');

// Wait for the specific search result item to appear
const targetItemSelector = '#docsearch-hits1-item-4';
await page.waitForSelector(targetItemSelector);

// Wait until the <a> element inside the target result is rendered with content
await page.waitForFunction(() => {
const item = document.querySelector('#docsearch-hits1-item-4');
const link = item?.querySelector('a');
return !!link && link.textContent.trim().length > 0;
});

// Click the target result link from within the page context
await Promise.all([
page.waitForNavigation({ waitUntil: 'networkidle0' }),
page.evaluate(() => {
const item = document.querySelector('#docsearch-hits1-item-4');
const link = item?.querySelector('a');
if (link) link.click();
}),
]);

// Wait for the title element to appear and extract its text content
const titleSelector = '#__docusaurus_skipToContent_fallback h1';
await page.waitForSelector(titleSelector);
const titleHandle = await page.$(titleSelector);
const fullTitle = titleHandle
? await page.evaluate(el => el.textContent, titleHandle)
: null;

// Print the page title after navigation
console.log(fullTitle);

// Close the browser
await browser.close();
})();
})();