Home > other >  Selenium userAgent
Selenium userAgent

Time:12-30

I want to open Google using

  • Pythons Selenium
  • Google Chrome
  • UserAgent (as iPhone)

The following code is working.

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('/Applications/chromedriver', options=chrome_options)
driver.get('https://www.google.com')

But I don't understand following line:

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

Why Mozilla and Mac OS X?

Is there a shorter version with just the standard code?

CodePudding user response:

User Agent

A User Agent is a software which retrieves, renders and facilitates end user interaction with the Web content.

Within the UserAgent:

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
  • Mozilla/5.0: is the general token that says that the browser is Mozilla-compatible. For historical reasons, almost every browser today sends it.

  • Mac OS X: The Chrome (or Chromium/Blink-based engines) user agent string is similar to Firefox. For compatibility, it adds strings like KHTML, like Gecko and Safari. Similarly, older Presto-based Opera releases used Mac OS.

  • Related