我想模拟移动设备以进行测试和自动化。使用 Chromedriver 和 Selenium,我找到了两种方法来做到这一点。我可以设置用户代理(此处的示例):
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--user-agent=Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3')
driver = webdriver.Chrome(chrome_options=options)
或者我可以启用移动仿真(此处的示例):
from selenium import webdriver
mobile_emulation = { "deviceName": "Google Nexus 5" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities = chrome_options.to_capabilities())
有什么不同?