在ChromeDriver中模拟移动设备 [英] Emulating mobile in 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
功能已在2.11中添加到ChromeDriver中
The mobile_emulation
capability was added to ChromeDriver in 2.11
完整文档: https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation
我在下面的笔记
使用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.
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屋!