Use mobile browser in selenium without emulating the device?
Passing correct user agent should do the trick. Example with mobile Chrome:
from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionschrome_options = Options()chrome_options.add_argument('--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1')driver = webdriver.Chrome(options=chrome_options)driver.get('https://www.google.com')
Specifying a Known Mobile Device
To enable Mobile Emulation with a specific device name, the “mobileEmulation” dictionary must contain a “deviceName.” Use a valid device name from the DevTools Emulation panel as the value for “deviceName.”
from selenium import webdrivermobile_emulation = { "deviceName": "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())
Specifying Individual Device Attributes
It is also possible to enable Mobile Emulation by specifying individual attributes. To enable Mobile Emulation this way, the “mobileEmulation” dictionary can contain a “deviceMetrics” dictionary and a “userAgent” string. The following device metrics must be specified in the “deviceMetrics” dictionary:
- “width” - the width in pixels of the device’s screen
- “height” - the height in pixels of the device’s screen
- “pixelRatio” - the device’s pixel ratio
- "touch" - whether to emulate touch events (defaults to true, usuallydoes not need to be set)
from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsmobile_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)