Playwrightã®ãªãªã¼ã¹ãã¼ãã«ãæ°ã«ãªãè¨è¼ãããã
Release v1.4.0 · microsoft/playwright · GitHub
è±èªãè¦æã§ãèªãããããã¿ããªå¤§å¥½ãdeepl翻訳ãããã¦ãããã
çéã®å¤è¨èªå±éã¹ãã¼ããæ¯ããä½ããããããããã¨ã¯ãããã
ãã ãå
·ä½çã«ã©ãããä»çµã¿ã§åãã¦ããã¨ããã©ãããã°ã¯ã©ã¤ã¢ã³ããå®è£
ã§ãããã¨ããããããæ
å ±ã¯ããã¥ã¡ã³ãã«ã¯ä¸åæ¸ããã¦ããªãã ããããã¤ã¯ãã½ããã ãªã®ã§ãããããã½ã¼ã¹ãèªãã§èª¿ã¹ãï¼å®éã«RubyããPlaywrightã使ãPoCãä½ã£ã¦ã¿ãã
Playwrightã¯Server/Clientã¢ã¸ã¥ã¼ã«ã«åããã¦ãã
çµè«ããè¨ãã¨ã
- microsoft/playwright (TypeScriptç)ã«ã¯Serverã¢ã¸ã¥ã¼ã«ã¨Clientã¢ã¸ã¥ã¼ã«ã®ä¸¡æ¹ãå«ã¾ãã¦ãã
- ãã ãREADMEãªã©ã«æ¸ããã¦ããåä½ã§ã¯ããµã¼ãã¼ã»ã¯ã©ã¤ã¢ã³ãåä½ã¯ç¹ã«ãããï¼ã¤ã®ããã»ã¹ã§èªååãåã
- microsoft/playwright-python, microsoft/playwright-java, microsoft/playwright-sharp, mxschmitt/playwright-go ã¯ãããããPlaywrightã®Clientã¢ã¸ã¥ã¼ã«ã§ãã
ãµã¼ãã¼ã¢ã¸ã¥ã¼ã«ã¨ããã®ã¯ä½ãã¨ããã¨ãPlaywrightèªèº«ãç¹å®ã®WebSocketã¾ãã¯ãã¤ãï¼æ¨æºå ¥å/æ¨æºåºåï¼ããã¤ã³ããã¦ãããã§åããå½ä»¤ããã£ãããã®ã¾ã¾ChromeãFirefoxãSafariã«ï¼ããæãã«å¤æãã¦ï¼æããåã
ã¯ã©ã¤ã¢ã³ãã¢ã¸ã¥ã¼ã«ã¨ããã®ã¯ãã¦ã¼ã¶ãæ¸ãã¹ã¯ãªããã§å®éã«ä½¿ãããPageã¨ãBrowserã¨ãElementHandleã¨ããã®ã¸ãã®ã¯ã©ã¹ã§ããµã¼ãã¼ã«å¯¾ãã¦ãWebSocketãªããã¤ããªãã§ãè¦æ±ãæããåã
- https://github.com/microsoft/playwright/tree/v1.7.1/src/server
- https://github.com/microsoft/playwright/tree/v1.7.1/src/client
PythonãJavaãC#ãGoã®ã¯ã©ã¤ã¢ã³ãã¯ãå é¨çã«playwright-cliã使ã£ã¦ãµã¼ãã¼ãèµ·åãã¦ãã
ãªãªã¼ã¹ãã¼ãã«ããNodeãããªãã¦ããåãããããã«ãããã¨æ¸ãã¦ãã£ãé¨åã®è©±ã
In the last release, we introduced an internal protocol to support Playwright in the none-Node environments
ã¿ã¤ãã«ã§ãã¿ãã¬ãã¦ãã¾ã£ãããããããã®ã¯ã©ã¤ã¢ã³ãã«ã¯
- playwright-cli (v1.8以éã¯playwright driver)ããã¦ã³ãã¼ãããé¨å
- playwright-cli run-driver ãå®è¡ããé¨å
ãããã
Pythonã§ããã°
- cliããã¦ã³ãã¼ãããé¨åâ https://github.com/microsoft/playwright-python/blob/v0.171.1/setup.py
- cli run-driverããé¨åâ https://github.com/microsoft/playwright-python/blob/v0.171.1/playwright/_transport.py
Goã§ããã°
- cliããã¦ã³ãã¼ã/run-driverããé¨åâ https://github.com/mxschmitt/playwright-go/blob/v0.171.1/run.go
試ãã«ãæå
㧠npx playwright-cli run-driver
ãã¦ã¿ããããªãã¨ããã ãã§Playwrightãµã¼ãã¼ãç«ã¡ä¸ããã®ããããã
ãã ãplaywright-pythonãplaywright-javaãªã©ã§ãã¦ã³ãã¼ãããã¦ããplaywright-cliã¯Nodeç°å¢ããªãã¦ãåãããã«ã·ã³ã°ã«ãã¤ããªï¼ã£ã½ãå½¢ã§é
å¸ããããã®ã®ããã ã playwright-pythonãjavaããã¦ã³ãã¼ããã¦ããURL㯠https://playwright.azureedge.net/builds/cli/next/playwright-cli-0.180.0-next.1608746109749-cbc13bd-mac.zip
ãããªæãã®ãã®ã§ãå®éã«ãããããã¦ã³ãã¼ããã¦ä¸èº«ãè¦ã¦ã¿ãâ
playwright-cli run-driverã¯Playwrightãµã¼ãã¼ã¢ã¸ã¥ã¼ã«ãèµ·åããã ãï¼
playwright-cli run-driverã®å é¨å®è£ ãä¸å¿ã¡ã¢ã£ã¦ããã
å®ã«åç´ã§ã
// Implement driver command. if (process.argv[2] === 'run-driver') runServer(); else if (process.argv[2] === 'print-api-json') printApiJson();
playwright-cli/cli.ts at v0.171.0 · microsoft/playwright-cli · GitHub
const { Playwright } = require('playwright/lib/server/playwright'); (ä¸ç¥) export function runServer() { installDebugController(); installTracer(); const dispatcherConnection = new DispatcherConnection(); const transport = new Transport(process.stdout, process.stdin); transport.onclose = async () => { // Force exit after 30 seconds. setTimeout(() => process.exit(0), 30000); // Meanwhile, try to gracefully close all browsers. await gracefullyCloseAll(); process.exit(0); }; transport.onmessage = (message: string) => dispatcherConnection.dispatch(JSON.parse(message)); dispatcherConnection.onmessage = (message: string) => transport.send(JSON.stringify(message)); const playwright = new Playwright(__dirname, require('playwright/browsers.json')['browsers']); (playwright as any).electron = new Electron(); new PlaywrightDispatcher(dispatcherConnection.rootDispatcher(), playwright); }
Playwrightãµã¼ãã¼ã¢ã¸ã¥ã¼ã«ãç´æ¥ã¤ã³ã¯ã«ã¼ããã¦èµ·åããéä¿¡è·¯ã¨ãã¦ãã¤ãï¼stdin/stdoutï¼ãæå®ãã¦ããã ãã
ãã¨ã¯ãPlaywrightãããã³ã«ãããã¹ãã¯ã©ã¤ã¢ã³ããæ¸ãã°ãã
ãµã¼ãã¼ã®ç«ã¡ä¸ãæ¹ãããã£ãã¨ããã§ããã¨ã¯æ¨æºå ¥å/æ¨æºåºåãä»ãã¦Playwrightã®ãããã³ã«ãããã¹ãã¯ã©ã¤ã¢ã³ããæ¸ãã°ããã ããã¨ãããã¨ã«ãªãã
ãã ãplaywright-pythonãplaywright-javaãã½ã¼ã¹ãè¦ã¦ã¿ãã¨ã¡ãã£ã¨å¤ãã£ãä½ãããã¦ãã¦ããããã³ã«ã®JSONããã¨ã«APIã¯ã©ã¤ã¢ã³ãã¤ã³ã¿ã¼ãã§ã¼ã¹ãèªåçæããããã«ãªã£ã¦ããã
npx playwright-cli print-api-json | jq .
ããããã¨ãã©ã£ã£ã°ã¼ã¼ã¼ã¼ã¼ã£ã¨APIã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®ç¾©ããJSONãéã£ã¦ãããplaywright-pythonãplaywright-javaããããé å¼µã£ã¦è§£æãã¦APIã¯ã©ã¤ã¢ã³ãã¤ã³ã¿ã¼ãã§ã¼ã¹ãçæãã¦ããã
ããããããã®ã¯Pythonã§ã ãã®ã¸ãã§ã½ã¼ã¹çæåããã¦ãå®éã«çæãããã½ã¼ã¹ã¯
- https://github.com/microsoft/playwright-python/blob/v0.171.1/playwright/async_api.py
- https://github.com/microsoft/playwright-python/blob/v0.171.1/playwright/sync_api.py
ãã®ãããã ã
ããã¨ã¯å¥ã«ãã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ é¨åããä½ã£ã¦ããã°ãã¯ã©ã¤ã¢ã³ãã¢ã¸ã¥ã¼ã«ãåºæ¥ä¸ããã
å®éã«Rubyã¯ã©ã¤ã¢ã³ãæ¸ãã¦ã¿ã
ãããã³ã«JSONãèªãã§ã³ã¼ãçæããé¨åãå°å³ã«å¤§å¤ã ã£ããã©ããã®ã¶ãå®è£ é¨åã¯ãã£ã¡ã楽ããªã«ãããã£ã¦ãããã
require 'playwright' Playwright.create(playwright_cli_executable_path: '/path/to/playwright-cli') do |playwright| playwright.chromium.launch(headless: false) do |browser| page = browser.new_page page.goto('https://github.com/YusukeIwaki') page.screenshot(path: './YusukeIwaki.png') end end
ãã®ãããã®ç°¡åãªã¹ã¯ãªãããåããã ããªããBrowser, BrowserType, Page, Frame ãªã©ã®ä¸»è¦ãªã¯ã©ã¹ã«ããã¤ãã®ã¡ã½ãããå®è£ ããã ãã§ãåãããã«ãªãã
puppeteer-rubyã®ã¨ãã«æ£ã è¦ããã ãCDPSessionã¾ããã®ä¸¦è¡å¦çã®é åºå¤ãã£ã¡ããåé¡ãªã©ã¯ä¸åãªããï¼ãã®ã¸ãã¯ãµã¼ãã¼ã¢ã¸ã¥ã¼ã«ããã£ã¦ãããã®ã§ï¼ï¼æ¬å½ã«ç´ ç´ã«ã¯ã©ã¤ã¢ã³ãæ¸ãã ãã ã
puppeteer-ruby ã®ã»ãã¯ãã°ããéçºããããã«ãã¦ãPlaywrightã®ã»ããæ¬è °å ¥ãã¦ä½ã£ã¦ãããããªã¨æãã