5

I am having an error when changing the web driver user agent in Python using selenium.

Here is my code:

import requests
import json
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Chrome(driver_path) 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'})
#Error is on line above

Here is my error:

>>> driver = webdriver.Chrome(driver_path) driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent":"python 3.7", "platform":"Windows"}) 
  File "<stdin>", line 1
    driver = webdriver.Chrome(driver_path) driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent":"python 3.7", "platform":"Windows"})```
Krystian G
  • 2,842
  • 3
  • 11
  • 25
Salvatore Timpani
  • 407
  • 3
  • 6
  • 26

3 Answers3

9

Your code is just perfect. You simply have to write the line of code to change the 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.36
    
  • Browser Snapshot:

useragent


Reference

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • `driver = webdriver.Chrome(executable_path=r'C:\Users\Salvatore\Desktop\Desktop\webdrivers\chromedriver.exe') ` This worked but I got this error and my browser shut down right away: `selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81` – Salvatore Timpani Jun 20 '20 at 22:04
  • 1
    @SalvatoreTimpani That is a different error which requires a different solution. Can you raise a new question for your new requirement please? – undetected Selenium Jun 20 '20 at 22:06
  • Here is my new post: https://stackoverflow.com/questions/62492419/selenium-error-this-version-of-chromedriver-only-supports-chrome-version-81 – Salvatore Timpani Jun 20 '20 at 22:28
2

You should use driver options:

from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("user-agent=[user-agent string]")

driver = webdriver.Chrome(executable_path='path', chrome_options=options)
Dmitry
  • 334
  • 1
  • 10
  • Is this syntax correct? ``` from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("user-agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'") ``` Also, would I keep the single quotes around the actual user agent or just keep the double quotes around the whole thing and that's fine? – Salvatore Timpani Jun 20 '20 at 21:57
  • 1
    No need to use single quotes. Just keep the double quotes around the whole thing. – Dmitry Jun 20 '20 at 23:08
0

follow the steps below :

1- you can use produce fake user-agent library in every request for use it

add to code :

from fake_useragent import UserAgent

2- and then in terminal do this :

 pip install fake_useragent

3- use in code : for example

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f'user-agent={userAgent}')
ua = UserAgent()
userAgent = ua.random
print(userAgent)
driver = webdriver.Chrome(options=chrome_options,executable_path=r"strin path 
chrome driver")
                 

and if you want from static user-agent use this code :

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--user-agent="Mozilla/5.0 (Windows NT 6.1; 
WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"')
driver = webdriver.Chrome(chrome_options=chrome_options)
              
mamal
  • 1,791
  • 20
  • 14