python之selenium设置浏览器为手机模式(开发者模式)

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()

 

posted @ 2022-03-09 11:42  博无止境  阅读(2006)  评论(0编辑  收藏  举报