1

I want to simulate mobile devices for testing and automation purposes. Using the Chromedriver and Selenium I have found 2 ways to do this. I can either set the user agent (example from here):

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)

Or I can enable mobile emulation (example from here):

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())

What's the difference?

Community
  • 1
  • 1
asemahle
  • 20,235
  • 4
  • 40
  • 38

1 Answers1

-3

User agent string: you are essentially lying to the web server about your browser.

Emulation mode: you are essentially lying to yourself about your browser.

SiKing
  • 10,003
  • 10
  • 39
  • 90