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

这篇博客介绍了如何使用Selenium进行浏览器模拟,包括设置为iPhone6手机模式和自定义分辨率。通过ChromeOptions添加实验性选项来实现手机模式,并提供了多种常见手机型号供选择。同时,展示了如何指定浏览器的宽度、高度和像素比,以模拟不同设备的显示效果。此外,还提及了开启开发者模式的选项。
摘要由CSDN通过智能技术生成

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()
开发者模式是指在使用selenium控制浏览器时,可以打开浏览器开发者工具。开发者工具提供了一系列的调试和分析工具,可以帮助开发人员进行网页开发和调试。在selenium中,可以通过设置选项来开启开发者模式。例如,在使用Chrome浏览器时,可以使用`--auto-open-devtools-for-tabs`参数来自动打开开发者工具。\[1\]\[3\]开启开发者模式后,可以通过开发者工具来查看请求信息、调试代码等。\[2\] #### 引用[.reference_title] - *1* [pythonselenium设置浏览器手机模式开发者模式)](https://blog.csdn.net/qq_42623386/article/details/123391709)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [selenium的使用与高级开发模式方法](https://blog.csdn.net/qq_42200107/article/details/124788812)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Python自动化 -- Selenium开启开发者工具(F12)和 设置手机模式](https://blog.csdn.net/zhfuce_2/article/details/128958054)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好度

你的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值