How to setup User-Agent in Python Selenium WebDriver

The Blog To Learn Selenium and Test Automation

How to setup User-Agent in Python Selenium WebDriver

In this article, lets see how we can setup user-agent for browsers and read user-agents in Python Selenium WebDriver. Manipulation of user-agent is required for many scenarios in testing.

What is User-Agent?

The User-Agent request header contains a characteristic string that allows the network protocol peers to identify the application type, operating system, software vendor or software version of the requesting software user agent. It appears in an HTTP Request Header and not applicable for response headers. It is supported by all browsers.

In short, User Agent is an identity of a client (user).

Common format for web browsers:

User-Agent: Mozilla/<version> (<system-information>) <platform> (<platform-details>) <extensions>

Example:
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:63.0) Gecko/20100101 Firefox/63.0

Selenium does not implement any direct methods to read request or response headers.

Note: For below examples to run, you need to set browser driver path details in PATH environment variable or have to pass browser driver path to executable_path variable while creating browser instance.

Get User-Agent value:

Selenium does not have any direct method to query the user-agent from an instance of WebDriver. We need to use execute javascript in-built method to do this and pass the script that returns user-agent.

Once the browser is started, we can get the user agent by executing following line of code

#Store it in a variable and print the value
agent = driver.execute_script("return navigator.userAgent")
print agent
#directly print the value
print driver.execute_script("return navigator.userAgent")

User-Agent setup in Firefox:

To change the user agent for Firefox browser, set variable “general.useragent.override” in Firefox profile and use this profile while creating Firefox WebDriver instance.

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "[user-agent string]")
#Below is tested line
#profile.set_preference("general.useragent.override", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:63.0) Gecko/20100101 Firefox/63.0")

driver = webdriver.Firefox(profile)

User-Agent setup in Chrome:

With Chrome, you have to use Options instance to set the user-agent value.

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

opts = Options()
opts.add_argument("user-agent=[user-agent string]")
#Below is tested line
#opts.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")

driver = webdriver.Chrome(chrome_options=opts)

Alternative way to pass driver path along with other details:

driver = webdriver.Firefox(profile, executable_path=”path to geckodriver”)

driver = webdriver.Chrome(chrome_options=opts, executable_path=”path to chromedriver”)

Note:
There is no standard way of writing a user-agent string; Different web browsers use different formats (some are wildly different), and many web browsers add lots of information into their user agents data.

Please provide your thoughts in comment section and stay tuned for steps to setup user-agent in other browsers!!!

 

One Response

  1. Muhammad Teguh says:

    Thank you, I use selenium python for scraping a banking website. But I think got blocked because I use robot. Then I add this user agent, and the problem fixed. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *