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 windows-10 system:
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 os 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 platform 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 :
First Execution :
userAgent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20130331 Firefox/21.0
Second Execution :
userAgent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko
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 :
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
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
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: