Skip to content
Advertisement

Use mobile browser in selenium without emulating the device?

I tried to write a script in python using selenium, that should automate uploading images to an image platform. However, the problem is, that the functionality for uploading pictures is only given, when you use a mobile browser (e.g. Safari on your iPhone). In a quick research, i found that selenium supports this, but as far as i understood it, this is only given if you emulate the device or connect a real device on your computer. Is there another way (maybe even another library?) for not having such an overhead (connecting or emulating the device), if you want to simulate an mobile browser using python?

Advertisement

Answer

Passing correct user agent should do the trick. Example with mobile Chrome:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_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')
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement