LoginSignup
0
0

Webブラウザ自動運転 - Playwright / Typescript

Last updated at Posted at 2024-04-12

(自分メモ)

環境

  • Ubuntu 22.04 on wsl2 on Windows 11
  • node: v21.7.3
  • npm: 10.5.0

プロジェクト初期化

プロジェクト履歴

bash
npm i playwright express
npm i -D @types/express

➜サンプルソース

src/main.ts
import Express from "express"
import Playwright from "playwright"

const app = Express()
app.get("/", runBrowser)
app.listen(8080, () => { console.log(`Running server.`); })

async function runBrowser(req: Express.Request, res: Express.Response) {
  const browser = await Playwright.chromium.launch({ headless: true })
  const page = await browser.newPage()
  await page.goto("https://digital.onl.jp/")
  const screenshot = await page.screenshot()
  res.setHeader("Content-Type", "image/png")
  res.send(screenshot)
  await browser.close()
}

Build

npm i         # clone後1回
npx tsc

Run

node build/main.js

ブラウザで http://localhost:8080/xxx にアクセス。

次回

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0