Skip to content

[LAB4] 313551136 #319

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

Merged
merged 1 commit into from
Jun 5, 2025
Merged
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
41 changes: 27 additions & 14 deletions lab4/main_test.js
Original file line number Diff line number Diff line change
@@ -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();
})();
})();
Loading