Playwright for Python を使うと iPhone / iPad / Pixel などのデバイスを使ったブラウザ操作を自動化できる.
ドキュメントに書いてある通り,playwright.devices
の Dict(辞書)にデバイス名を設定して,browser.new_context()
でコンテキストを作る.サンプルコードは後半に載せておく.簡単に自動化できるぞー❗️
コードを書きながら「デバイス名」をドキュメントから発見できず困ったけど,Playwright for Python でサポートしているデバイス名は GitHub の deviceDescriptorsSource.json
で確認できた.機種的には少し古い感じもするけど,iPhone / iPad / Pixel 以外に BlackBerry / Galaxy / Nexus なども選べるようになっていた.
iPhone 13 Pro
Apple 系のデバイス一覧を以下に挙げた.landscape
は「横置き」を意味する.
- iPad (gen 6)
- iPad (gen 6) landscape
- iPad (gen 7)
- iPad (gen 7) landscape
- iPad Mini
- iPad Mini landscape
- iPad Pro 11
- iPad Pro 11 landscape
- iPhone 6
- iPhone 6 landscape
- iPhone 6 Plus
- iPhone 6 Plus landscape
- iPhone 7
- iPhone 7 landscape
- iPhone 7 Plus
- iPhone 7 Plus landscape
- iPhone 8
- iPhone 8 landscape
- iPhone 8 Plus
- iPhone 8 Plus landscape
- iPhone SE
- iPhone SE landscape
- iPhone X
- iPhone X landscape
- iPhone XR
- iPhone XR landscape
- iPhone 11
- iPhone 11 landscape
- iPhone 11 Pro
- iPhone 11 Pro landscape
- iPhone 11 Pro Max
- iPhone 11 Pro Max landscape
- iPhone 12
- iPhone 12 landscape
- iPhone 12 Pro
- iPhone 12 Pro landscape
- iPhone 12 Pro Max
- iPhone 12 Pro Max landscape
- iPhone 12 Mini
- iPhone 12 Mini landscape
- iPhone 13
- iPhone 13 landscape
- iPhone 13 Pro
- iPhone 13 Pro landscape
- iPhone 13 Pro Max
- iPhone 13 Pro Max landscape
- iPhone 13 Mini
- iPhone 13 Mini landscape
まずは「iPhone 13 Pro」を試す.iPhone の場合はブラウザタイプに webkit
を選ぶ.
from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.webkit.launch() device = p.devices['iPhone 13 Pro'] context = browser.new_context(**device) page = context.new_page() page.goto('https://playwright.dev/python/') page.screenshot(path='images/playwright-device-iphone.png') context.close() browser.close()
スクリーンショットを載せておく📷
iPad Pro 11 landscape
次に「iPad Pro 11 landscape(横置き)」を試す.iPad の場合もブラウザタイプに webkit
を選ぶ.
from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.webkit.launch() device = p.devices['iPad Pro 11 landscape'] context = browser.new_context(**device) page = context.new_page() page.goto('https://playwright.dev/python/') page.screenshot(path='images/playwright-device-ipad.png') context.close() browser.close()
スクリーンショットを載せておく📷
Pixel 5
Pixel 系のデバイス一覧を以下に挙げた.
- Pixel 2
- Pixel 2 landscape
- Pixel 2 XL
- Pixel 2 XL landscape
- Pixel 3
- Pixel 3 landscape
- Pixel 4
- Pixel 4 landscape
- Pixel 4a (5G)
- Pixel 4a (5G) landscape
- Pixel 5
- Pixel 5 landscape
最後は「Pixel 5」を試す.Pixel の場合はブラウザタイプに chromium
を選ぶ.
from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.chromium.launch() device = p.devices['Pixel 5'] context = browser.new_context(**device) page = context.new_page() page.goto('https://playwright.dev/python/') page.screenshot(path='images/playwright-device-pixel.png') context.close() browser.close()
スクリーンショットを載せておく📷