如果您将 WebDriver 与 Chrome 一起使用(通过 Chromedriver),您可能想要模拟移动视口(viewport)特性。同样,您可能希望在桌面上自动化测试,而无需在 Android 设置上使用正确的 Chrome。
你是怎样做的?
最佳答案
mobile_emulation
功能在 2.11 中添加到 ChromeDriver
完整文档:https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation
我的笔记如下:
使用 mobile_emulation 功能选项在 Python 中创建驱动程序:
driver = self.CreateDriver(
mobile_emulation = {
'deviceMetrics': {'width': 360, 'height': 640, 'pixelRatio': 3}})
目前,您可以模拟 devicepixelratio、useragent、视口(viewport)高度和宽度。
Possible properties对于 mobile_emulation 字典:
deviceName
: 如果使用,必须是唯一的属性。匹配 device preset在 Chrome 中(例如 'Google Nexus 5'
)。 deviceMetrics
: 一个可以包含宽度(int)、高度(int)、pixelRatio(double)的字典,如上图所示。 userAgent
: 在请求头和导航器对象上欺骗的字符串。 关于mobile - 在 ChromeDriver 中模拟移动设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25394142/