Salvatore Timpani
Salvatore Timpani

Reputation: 457

How to change the User Agent using Selenium and Python

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"})```

Upvotes: 6

Views: 23393

Answers (3)

mamal
mamal

Reputation: 1976

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)
              

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193058

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:

Upvotes: 11

Dmitry
Dmitry

Reputation: 334

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)

Upvotes: 2

Related Questions