1

I'm building a bot that will change it's appearance based on the random user agent I select. I pull from a list of user agents, depending on the one selected, I change the capability of the webdriver and then launch it.

However I'm using https://ipleak.net/ to test, and no matter how many times I run this code my true operating system is still showing (instead of the supposedly spoofed version). Relevant code below, FYI true OS = MacIntel

desired_caps = DesiredCapabilities.CHROME.copy()
desired_caps['platform'] = user_agent_os

browser = webdriver.Chrome(options=options, desired_capabilities=desired_caps)
browser.get("https://ipleak.net/")

Possible values for user_agent_os = 'ios', 'windows', 'andriod', '---', 'symbian', 'macos', 'linux' and some others. The value is randomised each time the programme runs.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Heartthrob_Rob
  • 326
  • 4
  • 11
  • I'm wondering if I can do something fancy with Selenium-Wire to the headers, but I don't think that will change the webdriver capability. – Heartthrob_Rob Jul 16 '20 at 11:20
  • Also I believe the title is misleading, I want to change the operating system information within `platform` to match the `user-agent`, not the other way around – Heartthrob_Rob Jul 19 '20 at 06:24

1 Answers1

2

The platform property can be extracted from the WebDriver navigator.

You can use the following line of code to extract the value of navigator.platform and on my system:

  • Code Block:

    print("platform: "+driver.execute_script("return navigator.platform;"))
    
  • Console Output:

    platform: Win32
    

The possible values of Navigator platform Property are:

  • Mac68K
  • MacPPC
  • MacIntel
  • Linux i686
  • Linux armv7l
  • Win32
  • Win16
  • WinCE
  • SunOS
  • HP-UX

On the other hand, UserAgent i.e. navigator.userAgent does contain a information as:

  • Code Block:

    print("userAgent: "+driver.execute_script("return navigator.userAgent;"))
    
  • Console Output:

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

Solution

The only way to change the information within UserAgent is to rotate the UserAgent by using Python's fake_useragent module.

Chrome

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

options = Options()
ua = UserAgent()
options.add_argument(f'user-agent={ua.random}')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
print("userAgent: "+driver.execute_script("return navigator.userAgent;"))
driver.quit()

Result of 3 consecutive execution is as follows :

  1. First Execution :

    userAgent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20130331 Firefox/21.0
    
  2. Second Execution :

    userAgent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko
    
  3. Third Execution :

    userAgent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36
    

Firefox

from selenium import webdriver
from fake_useragent import UserAgent

useragent = UserAgent()
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", useragent.random)
driver = webdriver.Firefox(firefox_profile=profile, executable_path=r'C:\WebDrivers\\geckodriver.exe')
print("userAgent: "+driver.execute_script("return navigator.userAgent;"))
driver.quit()

Result of 3 consecutive execution is as follows :

  1. First Execution :

    userAgent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36 Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10
    
  2. Second Execution :

    userAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17
    
  3. Third Execution :

    userAgent: Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36
    

tl; dr

You can find a couple of relevant discussions on changing the useragent on the fly in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for your response, very detailed and I appreciate it. What I was actually after was how to change the `platform` value displayed by my computer to each website. Looking to match this to the `os` within `useragent` header. – Heartthrob_Rob Jul 18 '20 at 07:01
  • @Heartthrob_Rob That's exactly I explained how you need to spoof the `platform` value within the `useragent` header. – undetected Selenium Jul 20 '20 at 07:46
  • Hi @DebanjanB, I think I may have been unclear above. I am randomising the `useragent` from a list I have extracted. I want to then match my `platform` value to this user_agent. I believe your code above matches my `useragent` to `platform`, I am after the reverse. – Heartthrob_Rob Jul 21 '20 at 09:29