Skip to content

[LAB4] 313553801 #332

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

Closed
wants to merge 3 commits into from
Closed
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
44 changes: 33 additions & 11 deletions lab4/main_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const puppeteer = require('puppeteer');

(async () => {
// Launch the browser and open a new blank page
const browser = await puppeteer.launch();
Expand All @@ -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();
})();
})();

Loading