|
| 1 | +// Copyright 2023-2024 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Import the Chromium browser into our scraper. |
| 16 | +import { chromium } from 'playwright'; |
| 17 | + |
| 18 | +// browserAddress |
| 19 | +const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222'; |
| 20 | + |
| 21 | +// web serveur url |
| 22 | +const baseURL = process.env.BASE_URL ? process.env.BASE_URL : 'https://en.wikipedia.org/wiki/Web_browser'; |
| 23 | + |
| 24 | +// measure general time. |
| 25 | +const gstart = process.hrtime.bigint(); |
| 26 | +// store all run durations |
| 27 | +let metrics = []; |
| 28 | + |
| 29 | +// Connect to an existing browser |
| 30 | +console.log("Connection to browser on " + browserAddress); |
| 31 | +const browser = await chromium.connectOverCDP({ |
| 32 | + endpointURL: browserAddress, |
| 33 | + logger: { |
| 34 | + isEnabled: (name, severity) => true, |
| 35 | + log: (name, severity, message, args) => console.log(`${name} ${message}`) |
| 36 | + } |
| 37 | +}); |
| 38 | + |
| 39 | + |
| 40 | +const context = await browser.newContext({ |
| 41 | + baseURL: baseURL, |
| 42 | +}); |
| 43 | + |
| 44 | +const page = await context.newPage(); |
| 45 | +await page.goto(baseURL); |
| 46 | + |
| 47 | +const links = await page.getByRole('link').evaluateAll(links => |
| 48 | + links.map(link => link.getAttribute('href')) |
| 49 | +); |
| 50 | + |
| 51 | +console.log(links); |
| 52 | + |
| 53 | +await page.close(); |
| 54 | +await context.close(); |
| 55 | + |
| 56 | +// Turn off the browser to clean up after ourselves. |
| 57 | +await browser.close(); |
0 commit comments