Before Runtime
Setting a User Agent BEFORE RUNTIME is pretty straight forward using WebDriver Options / WebDriver Profiles that are available for Chrome (ChromeDriver) / Firefox (GeckoDriver).
You can invoke Google Chrome with a user agent using the command line and following command without even using Selenium: chrome --user-agent=Whatever
But if you want to use this with selenium you can give the same Arguments using the following Python Code for Google Chrome:
from selenium import webdriver
from selenium.webdriver.common.by import By
#Change Browser Options
option = webdriver.ChromeOptions()
option.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
#Open Browser
browser = webdriver.Chrome(executable_path='chromedriver.exe', options=option)
Firefox has a variable called general.useragent.override in your Firefox Profile.
You can make Selenium use a profile different from the default one and edit that with your new User Agent, using this Python Code:
from selenium import webdriver
#Change Profile
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "whatever you want")
#Open Browser
browser = webdriver.Firefox(profile)
At Runtime
As far as I know, you can only change your User Agent during runtime with Chrome because when you configure a GeckoDriver instance (Firefox) to start a new Firefox Browser Session, Firefox's configuration remains unchanged during the lifetime of the Firefox instance and remains unmodifiable.
But using the execute_cdp_cmd(cmd, cmd_args) command for Chrome you can easily execute google-chrome-devtools commands with Selenium - such as easily changing the user agent AT RUNTIME, for instance - or even changing your User Agent between each request without needing to restart.
See following Python Code:
from selenium import webdriver
driver = webdriver.Chrome(executable_path='chromedriver.exe')
#Print original User Agent
print(driver.execute_script("return navigator.userAgent;"))
# Set and print First Custom User Agent
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'YOUR FIRST USER AGENT HERE'})
print(driver.execute_script("return navigator.userAgent;"))
# Set and print Second Custom User Agent
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'YOUR SECOND USER AGENT HERE'})
print(driver.execute_script("return navigator.userAgent;"))
# Open httbin.org to see current user agent in Browser
driver.get('https://www.httpbin.org/headers')