在 ChromeDriver 中模拟移动设备 [英] Emulating mobile in ChromeDriver

查看:29
本文介绍了在 ChromeDriver 中模拟移动设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您将 WebDriver 与 Chrome 一起使用(通过 Chromedriver),您可能希望模拟移动视口特性.同样,您可能希望在桌面上自动执行测试,而无需在 Android 上使用正确的 Chrome 设置.

If you're using WebDriver with Chrome (via Chromedriver) you may want to emulate mobile viewport characteristics. Similarly, you may want to automate tests on desktop without having to use a proper Chrome on Android setup.

你是怎么做到的?

推荐答案

mobile_emulation 功能在 ChromeDriver 2.11 中添加

The mobile_emulation capability was added to ChromeDriver in 2.11

完整文档:https://sites.google.com/a/chromium.org/chromedriver/移动仿真

我的笔记如下:

使用 mobile_emulation 功能选项在 Python 中创建驱动程序:

Creating a driver in Python with the mobile_emulation capability options:

 driver = self.CreateDriver(
        mobile_emulation = {
            'deviceMetrics': {'width': 360, 'height': 640, 'pixelRatio': 3}})

目前您可以模拟 devicepixelratio、useragent、视口高度和宽度.

Currently you can emulate devicepixelratio, useragent, viewport height and width.

mobile_emulation 字典的可能属性:

  • deviceName :如果使用,必须是唯一的属性.匹配 设备预设(例如 'Google Nexus 5').
  • deviceMetrics:一个dict,可以包含width(int),height(int),pixelRatio(double)如上图.
  • userAgent:用于欺骗请求标头和导航器对象的字符串.
  • deviceName : if used, must be the only property. Matches a device preset in Chrome (e.g. 'Google Nexus 5').
  • deviceMetrics: a dict that can include width (int), height (int), pixelRatio (double) as shown above.
  • userAgent: a string to spoof at the request header and navigator object.

这篇关于在 ChromeDriver 中模拟移动设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆