在 ChromeDriver 中模拟手机 [英] Emulating mobile in ChromeDriver

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

问题描述

如果您在 Chrome 中使用 WebDriver(通过 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/移动仿真

我的笔记如下:

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

Creating a driver in Python with the mobile_emulation capability options:

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

目前您可以模拟设备像素比、用户代理、视口高度和宽度.

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

可能的属性 mobile_emulation dict:

Possible properties for the mobile_emulation dict:

  • deviceName :如果使用,必须是唯一的属性.匹配 Chrome 中的设备预设(例如 'Google Nexus 5').
  • deviceMetrics:一个字典,可以包含宽度(int)、高度(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天全站免登陆