How to set a Custom User Agent in Selenium?

On This Page What is a User Agent?

February 22, 2026 · 10 min read · Tool Comparison

How to set a Custom User Agent in Selenium?

A User Agentis a header string that identifies the system, browser, and twist when get a request to a server. It helps websites render optimized message by detecting mobile or desktop environments and adjusting images, menus, and style for better responsiveness.

By adjust content based on browser-device combinations, User Agents ensure smooth web coating performance across different platforms.

Overview

Use Cases for Custom User Agents in Selenium Testing

  • Testing Mobile Sites
  • Bypassing Restrictions
  • Testing Cross-Browser Behavior

The Impact of a User Agent on Website Rendering and Behavior

  • Smooth Rendering: Adjusts content for mobile or desktop free-base on the user agent.
  • Browser-Specific Features: Delivers compatible content by detecting the browser type.
  • OS-Specific Features: Optimizes interface rendering for different operating systems.

This guide explicate user agent, their significance, how to set and test them, best practices, and more.

What is a User Agent?

The user agent is a string of text contain information about the browser, twist, version, etc. The user agent twine is sent as a lintel while sending an HTTP request to the host. It allows smoother functionality by adjusting the message to the specific exploiter agents.

A User agent typically contains the info below:

  • Browser:Type of the Browser such as Chrome, Firefox, Safari, etc.
  • Browser Version: The version of the browser used.
  • Operating System: The operating system name such as Windows, macOS, Android, iOS, etc.
  • Device Type: Type of the gimmick such as mobile, desktop, tablet, etc.

Example of User agent

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

The above user agent string provides the below information

  • Browser:
  • Version:91.0
  • Rendering Engine: WebKit

Read More:

The Impact of a User Agent on Website Rendering and Behavior

User agents are very useful for responsive web pages and provide a bland user experience. Typically, contented bringing will be optimized based on the twist and browser types. It has the following advantages:

  • Smooth Rendering: The user agents help to display optimized content found on the device. For example, if the exploiter agent has a peregrine twist indication, websites render a mobile-friendly version instead of a desktop version.
  • Browser-specific characteristic: It allows the browser-specific features based on the browser type detected. For instance, Chrome might offer many different character of animations, while does not. By cognize the browser type, it is easy to deliver the content specific to the browser.
  • Operating System-Specific Features:The native interfaces may change based on the operating scheme. The user-agent helps to render and tune the interfaces according to the operating system that you are using.

Read More:

Why do you need to set a Custom User Agent in Selenium?

is a p popular exam automation framework that furnish flexibility to the tester. For example, the tester can tailor-make the browsers, user agents, and responsiveness.

User agent help to quiz the responsiveness and many other features utilize test automation. Below cursor explain the need for custom-made user agents in Selenium.

1. : The responsive design approach changes the webpage layout based on the twist type. It adjusts the screen breadth and height of the device.

The custom user agent simulates the website layout on several devices, such as mobile, pad, or desktop, and helps ensure it works without breaking.

2. Test functionality: Some features are available based on the device. However, it may be difficult to test all the features using physical device.

The exploiter agent facilitate in simulating specific device and versions, such as Android with Samsung Galaxy, iPhone 12, iPad, etc., habituate the desktop.

3. Bypassing: Custom user agent also help bypass specific restrictions ground on the device or browser type.

For example, some message may be blocked on mobile websites to ensure a suave experience. By direct the usage user agent, you can unblock such restrictions and test them.

4. Testing compatibility: While it may be difficult to install the different versions of the browser, by setting the custom user agent you can ensure that site are compatible with older versions of the browser.

5. Language/Location Specific features: The website may be available in diverse languages by nonpayment it may show the specific language base on the region. These features can be simulated utilize the user agents.

Read More:

How to set a Custom User Agent in Selenium WebDriver?

Selenium allows to set the custom exploiter agents. The browser driver can be configured to test custom user agents. Using Selenium custom exploiter agents can be set for any browser such as Chrome,, Firefox, etc.

Set custom exploiter agents for Chrome using Selenium

public nothingness testChrome () {ChromeOptions options = new ChromeOptions (); options.addArguments (`` user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 ''); WebDriver driver = new ChromeDriver (options); driver.get (`` https: //bstackdemo.com/ ''); driver.close ();}

In the above example, a custom exploiter agent is set for Chrome, and a ChromeOptions object is make and identify as an pick.

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

Then, an argument is added as a user agent to set the user agent using the options.addArguments () method.

Finally, a new Driver object is make with the delineate options usingnew ChromeDriver (option);.

Set impost user agents for Firefox utilise Selenium

@ Test public void testFirefox () {FirefoxProfile profile = new FirefoxProfile (); profile.setPreference (`` general.useragent.override '', `` Mozilla/5.0 (Android; Mobile) Gecko/68.0 Firefox/68.0 QwantMobile/3.5 ''); FirefoxOptions options = new FirefoxOptions (); options.setProfile (profile); WebDriver driver = new FirefoxDriver (options); driver.get (`` https: //bstackdemo.com/ ''); driver.close ();}

In the above example, a custom user agent is set for Firefox.

A FirefoxProfileobject is created to set the penchant.

Using the FirefoxProfileobject, the exploiter agent is overridden with the codeprofile.setPreference (& # 8220; general.useragent.override & # 8221;, & # 8220; & # 8221;);.

Then, you make theFirefoxOptionsobject to set the above profile(options.setProfile ()).

Finally, you can create theFirefoxDriverobject with these options.

Set custom exploiter agent for Edge using Selenium

Like Firefox and Chrome, You can too set the custom user agent for Edge in Selenium.

@ Test public vacuum testEdge () {EdgeOptions options = new EdgeOptions (); options.addArguments (`` user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 ''); WebDriver driver = new EdgeDriver (options); driver.get (`` https: //bstackdemo.com/ ''); driver.close ();}

In the above code, theEdgeOptionsobjective is take to add the arguing.

The options.addArguments ()method sets the user agent, and the selection object is legislate as a argument to make the new Edge Driver.

How to Test the Custom User Agent?

There are different means to verify the custom user agents. There are besides several websites that displays the Browser information found on the agents that you have set.

Consider that you are using the Selenium code below.

importee org.openqa.selenium.WebDriver; importation org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; signification org.openqa.selenium.firefox.FirefoxProfile; import org.testng.annotations.Test; public grade Demo {@ Test world void testFirefox () {FirefoxOptions options = new FirefoxOptions (); FirefoxProfile profile = new FirefoxProfile (); profile.setPreference (`` general.useragent.override '', `` Mozilla/5.0 (Android; Mobile) Gecko/68.0 Firefox/68.0 QwantMobile/3.5 ''); options.setProfile (profile); WebDriver driver = new FirefoxDriver (pick); driver.get (`` https: //www.whatismybrowser.com ''); driver.close ();}}

It can be verified that the custom user agents are set correctly on the website. In the above code, after the exploiter agent are set, the websitehttps: //www.whatismybrowser.comis sail to, where the browser name should be displayed as Android, as an Android and Qwant browser is being set.

Testing user agents manually

Once you set the user agents, you can manually see the web logs

RightClick & gt; Inspect & gt; Network & gt; Check any request headers of server responses

Talk to an Expert

Use Cases for Custom User Agents in Selenium Testing

There are many cases where usance agents are used in Selenium. As mentioned before it assist to formalize the responsiveness of the page, validate the device compatibility, verify the restrictions such as captcha, test cross-browser behavior, etc. Few of them are explained below.

Testing Mobile Sites

The server sends the answer free-base on the exploiter agent & # 8217; s answer. If you send the user agent an indication that the request is from mobile, the browser renders the mobile-specific content.

Example:

public nullity testChromeMobile () throws InterruptedException {ChromeOptions selection = new ChromeOptions (); options.addArguments (`` user-agent=Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 ''); WebDriver driver = new ChromeDriver (options); driver.get (`` https: //www.google.com/search? q=Browserstack ''); driver.close ();}

In the above representative, the Android 10 Mobile user agent is set. When Google.com hunting is pilot to, the mobile version of Google.com is rendered, as shown below.

Read More:

Bypassing Restrictions

Many websites will be restricted from scraping data when the exploiter agent is identified as a bot. Typically, the bot will be used to scrape large amounts of information from various website. Such restrictions can be bypassed by setting a impost user agent.

Execute the test by sending the bot user agent to realise the restrictions

@ Test public nothingness testChrome () throws InterruptedException {ChromeOptions options = new ChromeOptions (); options.addArguments (`` user-agent=Mozilla/5.0 (compatible; bingbot/2.0; +http: //www.bing.com/bingbot.htm) ''); WebDriver driver = new ChromeDriver (options); driver.get (`` https: //www.medium.com ''); driver.close ();}

In the above example, the bot user agent go to Bingbot is sent. Once the medium.com website is navigated to, the website is blocked as bots are not allowed to crawl the web page.

You can remove the restrictions by setting a Firefox exploiter agent.

@ Test world nothingness testChrome () cast InterruptedException {ChromeOptions pick = new ChromeOptions (); options.addArguments (`` user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0 ''); WebDriver driver = new ChromeDriver (options); driver.get (`` https: //www.medium.com ''); driver.close ();}

The above code sets the Firefox user agent to bypass the restrictions. Upon executing it, you will see that medium.com is laden.

Note:The above model is for demonstration purposes. If you are scrape data, make sure you have read the terms of service. Scraping the information without the possessor & # 8217; s permission and using custom-made agent or any ward-heeler is illegal.

Testing Cross-Browser Behaviour

User agents can also be used to try the cross-browser behaviour of web applications. You can mimic the user agent of any browser.

Example:Sending the Safari User Agent on Chrome

@ Test public void testChrome () {ChromeOptions options = new ChromeOptions (); options.addArguments (`` user-agent=Mozilla/5.0 (Windows; U; Windows NT 6.1; ko-KR) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27 ''); WebDriver driver = new ChromeDriver (selection); driver.get (`` https: //www.browserstack.com ''); driver.close ();}

In the above codification, the Safari exploiter agent is post

Example:Sending the Firefox User Agent on Chrome

@ Test public void testChrome () {ChromeOptions option = new ChromeOptions (); options.addArguments (`` user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0 ''); WebDriver driver = new ChromeDriver (options); driver.get (`` https: //www.browserstack.com ''); driver.close ();}

The above codification uses the Firefox browser user agent which is of adaptation 135.0.

Best Practices for using Custom User Agents in Selenium

Here are some of the better practices for habituate custom user agents:

  • Do not violate the terms of service: You must ensure that if you are bypassing restrictions, it is allowed to bypass. Scraping data without permission can violate effectual agreements and may result in strict actions.
  • Ensure the exploiter agent is realistic: The user agent should be set in a naturalistic way such as device, version, and browser should be properly given.
  • Simulate the Real User Behaviour: Rather than sending too many requests with random exploiter agents, use conditions and delay to mime the real user behavior.
  • Use existent devices to test: Rather than simulating wandering device, different operating systems use cloud providers like to test your site on real device. The user-agent-based simulations may not always be exact. Using real device helps to get the right results.

Why choose BrowserStack to test custom exploiter agents in Selenium?

While custom user agents can help simulate different devices and browsers, they do not fully replicate real-world weather. Factors like screen resolve, operating system behavior, touch interactions, and hardware variations likewise impact website performance.

With, you can quiz across existent devices and browsers rather than relying only on user-agent model. This gives exact, dependable, and comprehensive exam results. Additionally, Selenium integrating is easier, allowing you to run test on BrowserStack without modifying your existing scripts.

Conclusion

Custom user agent help validate web page content quickly and allow us to cognise how application behave under different conditions.

However, considering and compatibility testing, the custom user agents may not provide accurate results. is a cloud-based test execution platform that provides many real devices, including desktop, web, and mobile.

Additionally, you are flexible to choose the different browsers such as Chrome, Safari, Firefox, Edge, etc. can likewise help to automate such scenario by integrating popular test automation frameworks including Selenium.

Additionally, BrowserStack Automate can also execute,, cross-device examination,, and.

Tags
55,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free