Way to change Google Chrome user agent in Selenium?
A simple way to use a random User Agent would be using Python's fake_useragent
module as follows :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent
options = Options()
ua = UserAgent()
userAgent = ua.random
print(userAgent)
options.add_argument(f'user-agent={userAgent}')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\ChromeDriver\chromedriver_win32\chromedriver.exe')
driver.get("https://www.google.co.in")
driver.quit()
Result of 3 consecutive execution is as follows :
First Execution :
Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
Second Execution :
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36
Third Execution :
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
Change user agent using selenium wire in Chrome
from seleniumwire import webdriver # Import from seleniumwire
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--user-agent="Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166"')
browser = webdriver.Chrome(chrome_options=chrome_options)
user_agent = browser.execute_script("return navigator.userAgent;")
print(str(user_agent))
# Go to the Google home page
browser.get('https://www.google.com')
The same Chrome options that are mentioned in this question will also work here. For the printing of user-agent string see this question.
How to change the User Agent using Selenium and Python
Your code is just perfect. You simply have to write the line of code to change the user-agent in the next line. As an example:
Code Block:
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
print(driver.execute_script("return navigator.userAgent;"))
# Setting user agent 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;"))
# Setting user agent as Chrome/83.0.4103.53
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.53 Safari/537.36'})
print(driver.execute_script("return navigator.userAgent;"))
driver.get('https://www.httpbin.org/headers')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
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36Browser Snapshot:
Reference
You can find a couple of relevant detailed discussions in:
- How to rotate various user agents using selenium python on each request
- Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection
How to set a custom name for the user-agent using Selenium and Python
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 user-agent 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:
- Way to change Google Chrome user agent in Selenium?
- How to change the User Agent using Selenium and Python
- How to rotate various user agents using selenium python on each request
How to change the Google Chrome UserAgent using the ChromeDriver installed through webdriver_manager
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>
webdriver_manager.chrome
webdriver_manager.chrome is the python module to help users to download and deploy WebDriver binaries. The classes in this module can be used to automatically search for and download the latest version (or a specific version) of a WebDriver binary and then extract it and place it by copying or symlinking it to the location where Selenium or other tools should be able to find it then.
Summary
To summarize, from the above points it can be deduced that:
- user-agent is implemented through the request header and deals with the native browser.
- webdriver-manager is a Python module to deal with downloading and installing WebDriver binaries.
Hence, they aren't interrelated and can be implemented combinedly as follows:
Code Block:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://duckduckgo.com/')
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;"))
References
You can find a couple of relevant detailed discussions in:
- Way to change Google Chrome user agent in Selenium?
- How to change the User Agent using Selenium and Python
- How to rotate various user agents using selenium python on each request
How do I modify Google Chrome user agent string with Robot Framework
After some more tinkering and reading I managed to figure out a way to get the example working.
Instead of using ${options.add_argument}=
I used Call Method ${options} add_argument
.
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${options} add_argument --user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 System/ComputerId
Create WebDriver Chrome chrome_options=${options}
Go To http://www.useragentstring.com
Related Topics
Parallel Processing from a Command Queue on Linux (Bash, Python, Ruby... Whatever)
"Getaddrinfo Failed", What Does That Mean
Python Date String to Date Object
Python: Access Class Property from String
Can Pandas Automatically Read Dates from a CSV File
Multiple Python Versions on the Same MAChine
Some Unix Commands Fail with "<Command> Not Found", When Executed Using Python Paramiko Exec_Command
How to Check Whether a File Is Empty or Not
Finding Max Value in the Second Column of a Nested List
How to Execute a File Within the Python Interpreter
Why Is Dictionary Ordering Non-Deterministic
Windows Scipy Install: No Lapack/Blas Resources Found
What Is Different Between Makedirs and Mkdir of Os
Python: Get Output of the Shell Command 'History'
In Python, How to Read the Exif Data for an Image