- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change user agent for Selenium driver?
We can change the user Agent for Selenium webdriver. The user Agent header has a particular string that provides the network protocol along with the details of operating system, software version, application, and so on.
To obtain the user Agent information, we have to pass the return navigator.userAgent parameter to that method. To change the user Agent, we shall take the help of ChromeOptions class.
Then apply the add_argument method on the object created. We shall pass user-agent and <value of the user Agent> as parameters to that method. Finally, this information shall be passed to the driver object.
op = webdriver.ChromeOptions() op.add_argument("user-agent=<user agent value>")
from selenium import webdriver from selenium.webdriver.chrome.options import Options #object of Options class op = webdriver.ChromeOptions() #add user Agent op.add_argument ("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64)" +"AppleWebKit/537.36 (KHTML, like Gecko)" +"Chrome/87.0.4280.141 Safari/537.36") #set chromedriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe", options=op) #maximize browser driver.maximize_window() #launch URL driver.get("https://www.seleniumhq.org/download/"); #get user Agent with execute_script a= driver.execute_script("return navigator.userAgent") print("User agent:") print(a) #close browser session driver.quit()
- Related Articles
- What are the pre-conditions for Selenium Internet Explorer Driver or IE Driver?
- How to get userAgent information in Selenium Web driver?
- How to setup Chrome driver with Selenium on MacOS?
- How to change the password in MongoDB for existing user?
- What is a User Agent (UA)
- What is Selenium Internet Explorer Driver or IE Driver?
- How to detect Android user agent type in general using kotlin?
- How do detect Android user agent type in general?
- What is selenium web driver?
- How to get text with selenium web driver in python?
- How to get innerHTML of whole page in selenium driver?
- How to run selenium (Firefox) web driver without a GUI?
- What is Web Driver in Selenium?
- How do I pass options to the Selenium Chrome driver using Python?