I recently had an issue where I couldn't change my user-agent in selenium. I got that fixed but then had a chrome driver issue where it required chrome driver version 81. I got that fixed but don't know how to implement the user-agent solution into the chrome driver-specific code.|
Here is the code:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
Before, this is how I was changing the user agent:
from selenium import webdriver
# Below is where my code would give me the chrome driver error. So I used the solution above for that.
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
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'})
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.
The common format for web browsers is as follows:
User-Agent: Mozilla/5.0 (<system-information>) <platform> (<platform-details>) <extensions>
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.
To summarize, from the above points it can be deduced that:
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;"))
You can find a couple of relevant detailed discussions in: