Skip to content

Commit 3011992

Browse files
authored
Merge pull request #349 from KentYang123/lab4
[LAB4] 512558014
2 parents 6c32128 + 7a8ef22 commit 3011992

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

lab4/main_test.js

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,47 @@ const puppeteer = require('puppeteer');
55
const browser = await puppeteer.launch();
66
const page = await browser.newPage();
77

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-
})();
8+
try {
9+
// Navigate the page to a URL
10+
await page.goto('https://pptr.dev/');
11+
12+
// Hints:
13+
// Click search button
14+
// Type into search box
15+
// Wait for search result
16+
// Get the `Docs` result section
17+
// Click on first result in `Docs` section
18+
// Locate the title
19+
// Print the title
20+
21+
// Click search button
22+
await page.click('button.DocSearch.DocSearch-Button');
23+
24+
// Type into search box
25+
await page.waitForSelector('#docsearch-input');
26+
await page.type('#docsearch-input', 'chipi chipi chapa chapa', { delay: 1000 });
27+
28+
// Wait for search result
29+
await page.waitForSelector('#docsearch-item-5');
30+
31+
// Click on first result in `Docs` section
32+
await page.click('#docsearch-item-5');
33+
34+
// Locate the title
35+
await page.waitForSelector('h1');
36+
37+
// Get the text content of the title element
38+
const title = await page.evaluate(() => {
39+
const titleElement = document.querySelector('h1');
40+
return titleElement.textContent;
41+
});
42+
43+
// Print the title
44+
console.log(title);
45+
} catch (error) {
46+
console.error('An error occurred:', error);
47+
} finally {
48+
// Close the browser
49+
await browser.close();
50+
}
51+
})();

0 commit comments

Comments
 (0)