Selenium 模拟不同型号设备访问

前言

        做自动化测试的同学都知道,谷歌浏览器的F12开发者模式,是可以模拟不同的移动设备进行访问网站。我试图获取到预设的手机型号列表,但没有找到对的方法,所以只能按名称去设置,那如何需要在Selenium中去使用这个功能?

使用方法

    使用预设型号

    通过mobileEmulation参数支持直接调用设备名称,但需‌确保名称与Chrome内置列表完全一致。

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', {'deviceName': 'iPhone 12 SE'})  
driver = webdriver.Chrome(options=options)

    使用自定义型号

    可改用deviceMetrics(分辨率、像素比)和userAgent参数自定义‌。需要想办法获取到userAgent。

from selenium import webdriver

options = webdriver.ChromeOptions()
mobile_emulation = {
    "deviceMetrics": {"width": 375, "height": 667, "pixelRatio": 2.0},
    "userAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1"
}
options.add_experimental_option('mobileEmulation', mobile_emulation)
driver = webdriver.Chrome(options=options)

相关推荐

最全ChromeDriver下载含win linux mac 最新版本134.0.6998.165 持续更新..._chromedriver 134-CSDN博客

对你有用,就点个赞把!

对你有用,就点个赞把!

对你有用,就点个赞把!

你可能感兴趣的:(selenium,测试工具)