python之selenium設定瀏覽器為手機模式(開發者模式)
阿新 • • 發佈:2022-03-09
1. 啟動手機模式瀏覽器, 手機型號只能選以下範圍。
from selenium.webdriver.chrome.options import Options# 手機模式 # 設定手機型號,這設定為iPhone 6 mobile_emulation = {"deviceName": "iPhone 6"} options = Options() options.add_experimental_option("mobileEmulation", mobile_emulation) # 啟動配置好的瀏覽器 driver = webdriver.Chrome(options=options)# 輸入網址 driver.get('http://m.baidu.com') time.sleep(3) driver.close()
常用手機型號列表
mobile_emulation = { "deviceName": "Apple iPhone 3GS", "deviceName": "Apple iPhone 4", "deviceName": "Apple iPhone 5", "deviceName": "Apple iPhone 6", "deviceName": "Apple iPhone 6 Plus", "deviceName": "BlackBerry Z10", "deviceName": "BlackBerry Z30", "deviceName": "Google Nexus 4", "deviceName": "Google Nexus 5", "deviceName": "Google Nexus S", "deviceName": "HTC Evo, Touch HD, Desire HD, Desire", "deviceName": "HTC One X, EVO LTE", "deviceName": "HTC Sensation, Evo 3D", "deviceName": "LG Optimus 2X, Optimus 3D, Optimus Black", "deviceName": "LG Optimus G", "deviceName": "LG Optimus LTE, Optimus 4X HD" , "deviceName": "LG Optimus One", "deviceName": "Motorola Defy, Droid, Droid X, Milestone", "deviceName": "Motorola Droid 3, Droid 4, Droid Razr, Atrix 4G, Atrix 2", "deviceName": "Motorola Droid Razr HD", "deviceName": "Nokia C5, C6, C7, N97, N8, X7", "deviceName": "Nokia Lumia 7X0, Lumia 8XX, Lumia 900, N800, N810, N900", "deviceName": "Samsung Galaxy Note 3", "deviceName": "Samsung Galaxy Note II", "deviceName": "Samsung Galaxy Note", "deviceName": "Samsung Galaxy S III, Galaxy Nexus", "deviceName": "Samsung Galaxy S, S II, W", "deviceName": "Samsung Galaxy S4", "deviceName": "Sony Xperia S, Ion", "deviceName": "Sony Xperia Sola, U", "deviceName": "Sony Xperia Z, Z1", "deviceName": "Amazon Kindle Fire HDX 7″", "deviceName": "Amazon Kindle Fire HDX 8.9″", "deviceName": "Amazon Kindle Fire (First Generation)", "deviceName": "Apple iPad 1 / 2 / iPad Mini", "deviceName": "Apple iPad 3 / 4", "deviceName": "BlackBerry PlayBook", "deviceName": "Google Nexus 10", "deviceName": "Google Nexus 7 2", "deviceName": "Google Nexus 7", "deviceName": "Motorola Xoom, Xyboard", "deviceName": "Samsung Galaxy Tab 7.7, 8.9, 10.1", "deviceName": "Samsung Galaxy Tab", "deviceName": "Notebook with touch", "deviceName": "iPhone 6"}
2. 指定解析度,自定義寬高
import time from selenium import webdriver # 設定寬高解析度 WIDTH = 412 # 寬度 HEIGHT = 915 # 高度 PIXEL_RATIO = 3.0 # 解析度 # 配置瀏覽器引數 mobileEmulation = {"deviceMetrics": {"width": WIDTH, "height": HEIGHT, "pixelRatio": PIXEL_RATIO}} options = webdriver.ChromeOptions() options.add_experimental_option('mobileEmulation', mobileEmulation) # 啟動配置好引數的瀏覽器 driver = webdriver.Chrome(options=options) # 開啟網址 driver.get('http://m.baidu.com') time.sleep(3) driver.close()
3. 開啟瀏覽器的(開發者模式)。手機模式和開發者模式可以一起用
import time from selenium import webdriver options = webdriver.ChromeOptions() # 手機模式 # mobile_emulation = {"deviceName": "iPhone 6"} # options.add_experimental_option("mobileEmulation", mobile_emulation) # 開發者模式 options.add_argument("--auto-open-devtools-for-tabs") driver = webdriver.Chrome(options=options) driver.get('http://m.baidu.com') time.sleep(3) driver.close()