2
3

More than 3 years have passed since last update.

【EC2】seleniumでスマホの画面キャプチャを撮る方法

Posted at

【EC2】seleniumでスマホの画面キャプチャを撮る方法

スマホ用のUAをoptionsの引数に追加すればOK。

▼iphone safariの場合

追加するオプション
options.add_argument('--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0 Mobile/14C92 Safari/602.1')

▼コード全体例

#-*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True

#キャプチャする画面サイズを指定
options.add_argument('--window-size=412,732')
####driver.set_window_size(600,1000)

#UAを指定
options.add_argument('--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0 Mobile/14C92 Safari/602.1')

driver = webdriver.Chrome(options=options)

#URLを指定
driver.get("https://www.google.co.jp/")

#キャプチャのファイル名と拡張子を指定
driver.save_screenshot('googletop-sp.png')


driver.quit()


主要なユーザーエージェントの一覧

Android(Google WebLight Proxy)

Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko; googleweblight) Chrome/38.0.1025.166 Mobile Safari/535.19

image.png

mobile用とmobile Phone用がある。

iphone(Safari 13)

Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1
2
3
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
2
3