Skip to content

Commit dfb4463

Browse files
committed
complete lab4
1 parent 2bfb4e2 commit dfb4463

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

lab4/main_test.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
const puppeteer = require('puppeteer');
22

33
(async () => {
4-
// Launch the browser and open a new blank page
5-
const browser = await puppeteer.launch();
6-
const page = await browser.newPage();
7-
8-
// Navigate the page to a URL
9-
await page.goto('https://pptr.dev/');
10-
11-
// Hints:
12-
// Click search button
13-
// Type into search box
14-
// Wait for search result
15-
// Get the `Docs` result section
16-
// Click on first result in `Docs` section
17-
// Locate the title
18-
// Print the title
19-
20-
// Close the browser
21-
await browser.close();
22-
})();
4+
const browser = await puppeteer.launch();
5+
const page = await browser.newPage();
6+
7+
await page.goto('https://pptr.dev/');
8+
await page.setViewport({ width: 1080, height: 1024 });
9+
10+
await page.click('button.DocSearch.DocSearch-Button');
11+
await page.waitForSelector('input#docsearch-input.DocSearch-Input');
12+
13+
await page.type('input#docsearch-input.DocSearch-Input', 'andy popoo');
14+
15+
await page.waitForSelector('.DocSearch-Hit');
16+
17+
const links = await page.$$('.DocSearch-Hit a');
18+
19+
for (const link of links) {
20+
const text = await link.evaluate(el => el.textContent.trim());
21+
if (text === 'ElementHandle.dragAndDrop() method') {
22+
const href = await link.evaluate(el => el.href);
23+
await page.goto(href);
24+
break;
25+
}
26+
}
27+
28+
const titleElement = await page.waitForSelector('h1');
29+
const fullTitle = await titleElement.evaluate(el => el.textContent.trim());
30+
console.log(fullTitle);
31+
32+
await browser.close();
33+
})();

0 commit comments

Comments
 (0)