Skip to content

Commit 2f4372a

Browse files
authored
Merge pull request #319 from NLP-cabbage/lab4
[LAB4] 313551136
2 parents b785197 + fde9dde commit 2f4372a

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

lab4/main_test.js

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

33
(async () => {
4-
// Launch the browser and open a new blank page
5-
const browser = await puppeteer.launch();
4+
const browser = await puppeteer.launch({
5+
headless: 'new',
6+
});
67
const page = await browser.newPage();
7-
8-
// Navigate the page to a URL
8+
await page.setViewport({ width: 1280, height: 800 });
99
await page.goto('https://pptr.dev/');
1010

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
11+
await page.click('button.DocSearch-Button');
12+
13+
await page.waitForSelector('input.DocSearch-Input');
14+
await page.click('input.DocSearch-Input');
15+
const keyword = 'andy popoo';
16+
await page.keyboard.type(keyword, { delay: 100 });
17+
18+
await new Promise(resolve => setTimeout(resolve, 2000));
19+
20+
const links = await page.evaluate(() => {
21+
return Array.from(document.querySelectorAll('.DocSearch-Hit a')).map(a => a.textContent.trim());
22+
});
23+
24+
const targetIndex = links.findIndex(text => text.includes('ElementHandle.dragAndDrop() method'));
25+
await page.evaluate((index) => {
26+
const allLinks = Array.from(document.querySelectorAll('.DocSearch-Hit a'));
27+
allLinks[index].click();
28+
}, targetIndex);
29+
30+
await page.waitForSelector('h1');
31+
const title = await page.$eval('h1', el => el.textContent);
32+
console.log(title);
1933

20-
// Close the browser
2134
await browser.close();
22-
})();
35+
})();

0 commit comments

Comments
 (0)