如何使用Selenium在移动仿真模式下打开Chrome?
根据
these docs
您应该能够在移动仿真模式下启动Chrome,您通常可以通过Inspect面板访问该模式,使用:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
mobile_emulation = {
"deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options=chrome_options)
然而,用最新版本的Chrome和ChromeDriver进行测试表明,它不起作用。它可以打开Chrome浏览器,但它处于正常的桌面模式。所有的移动选项似乎都被完全忽略了。
我仍然可以在Selenium创建的浏览器中手动启用移动仿真,但这对自动化测试没有帮助。