-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathmain_test.js
More file actions
57 lines (50 loc) · 1.89 KB
/
main_test.js
File metadata and controls
57 lines (50 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const puppeteer = require('puppeteer');
(async () => {
// Launch the browser and open a new blank page
const browser = await puppeteer.launch({
headless: false,
slowMo: 1
});
const page = await browser.newPage();
// Navigate the page to a URL
try {
await page.goto('https://pptr.dev/');
await page.setViewport({ width: 1080, height: 1024 });
// Click search button
await page.click('.DocSearch-Button');
await page.waitForSelector('.DocSearch-Input');
// Type into search box
// Wait for search result
await page.type('.DocSearch-Input', 'Andy popoo');
// Get the `Docs` result section
// Click on first result in `Docs` section
await page.waitForFunction(() => {
const bits = document.querySelectorAll('.DocSearch-Hit-source');
return Array.from(bits).some(el =>
el.textContent.trim() === 'ElementHandle'
);
}, { timeout: 8000 });
await page.evaluate(() => {
const bits = document.querySelectorAll('.DocSearch-Hit-source');
for (const bite of bits) {
if (bite.textContent.trim() === 'ElementHandle') {
const parent = bite.parentElement;
const first = parent.querySelector('.DocSearch-Hit a');
if (first) {
first.click();
return true;
}
}
}
throw new Error('No results found for elementhandle');
});
// locate and print the title
await page.waitForSelector('h1');
const header = await page.$eval('h1', el => el.textContent.trim());
console.log(header);
} catch (error) {
console.error('Error:', error.message);
} finally {
await browser.close();
}
})();