-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathmain_test.js
More file actions
58 lines (47 loc) · 2.17 KB
/
main_test.js
File metadata and controls
58 lines (47 loc) · 2.17 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
58
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless:false});
const page = await browser.newPage();
// Set viewport size
await page.setViewport({ width: 1080, height: 1024 });
// Navigate to the Puppeteer official website
await page.goto('https://pptr.dev/');
// Click the search button in the top-right navbar
const searchBtnSelector =
'#__docusaurus > nav > div.navbar__inner > div.navbar__items.navbar__items--right > div.navbarSearchContainer_IP3a > button > span.DocSearch-Button-Container > svg';
await page.waitForSelector(searchBtnSelector);
await page.click(searchBtnSelector);
// Type search query into the search input
const searchInputSelector = '#docsearch-input';
await page.waitForSelector(searchInputSelector);
await page.type(searchInputSelector, 'andy popoo');
// Wait for the specific search result item to appear
const targetItemSelector = '#docsearch-hits1-item-4';
await page.waitForSelector(targetItemSelector);
// Wait until the <a> element inside the target result is rendered with content
await page.waitForFunction(() => {
const item = document.querySelector('#docsearch-hits1-item-4');
const link = item?.querySelector('a');
return !!link && link.textContent.trim().length > 0;
});
// Click the target result link from within the page context
await Promise.all([
page.waitForNavigation({ waitUntil: 'networkidle0' }),
page.evaluate(() => {
const item = document.querySelector('#docsearch-hits1-item-4');
const link = item?.querySelector('a');
if (link) link.click();
}),
]);
// Wait for the title element to appear and extract its text content
const titleSelector = '#__docusaurus_skipToContent_fallback h1';
await page.waitForSelector(titleSelector);
const titleHandle = await page.$(titleSelector);
const fullTitle = titleHandle
? await page.evaluate(el => el.textContent, titleHandle)
: null;
// Print the page title after navigation
console.log(fullTitle);
// Close the browser
await browser.close();
})();