3

I am using selenium + webdriver and trying testing different user agents. I am adding user agent like this for Chrome on Windows for example:

option = Options()
option.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36")

Now when I log see login details it says Windows Chrome but when I want to rename it to something else like this:

option.add_argument("user-agent=test-user-agent")

or

option.add_argument("user-agent=Mozilla/5.0 (test-user-agent NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36")

Some websites display it as unknown or browser not supported

Is there a way to "rename" user-agent or create custom one or there is only preset number of them that websites know?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
cheshire
  • 1,109
  • 3
  • 15
  • 37
  • 1
    Here is a massive list of user-agent strings you can use: http://www.useragentstring.com/pages/useragentstring.php – Aero Blue Jun 21 '20 at 17:46

1 Answers1

4

User-Agent

The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.


Syntax

The common format for web browsers is as follows:

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

This usecase

While your first code attempt to add a specific would work perfect:

  • Code Block:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36")
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    print(driver.execute_script("return navigator.userAgent;"))
    
  • Console Output:

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

But as per your second attempt you can't rename the User-Agent as it violates the prescribed format/syntax.


However, you can always change the User-Agent using the execute_cdp_cmd(cmd, cmd_args) as follows:

  • Code Block:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36")
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    print(driver.execute_script("return navigator.userAgent;"))
    # Setting UserAgent as Chrome/83.0.4103.97
    driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'})
    print(driver.execute_script("return navigator.userAgent;"))
    
  • Console Output:

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

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352