41

I'm trying to figure out a way whereby whenever I open up Chrome via Selenium (in Python) in this particular script, the Chrome page automatically opens up with another user agent selected - in this case, Microsoft Edge Mobile (but I will be accessing it from the desktop).

So, after doing some research, I've been able to piece together the following code, which I thought would execute a user-agent switch in Chrome and then open up a new Bing.com page:

from selenium import webdriver 
from selenium.webdriver.chrome.options

import Options opts = Options()
opts.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")
driver = webdriver.Chrome(chrome_options=opts)
driver = webdriver.Chrome("D:\_")
driver.get("https://www.bing.com/")

However, the code doesn't seem to be working and stops before opening up the designated webpage. I'm fairly certain the first half of code is off, but I'm not quite sure how. Any and all help would be deeply appreciated.

Nick D
  • 401
  • 3
  • 15
theCrabNebula
  • 421
  • 2
  • 9
  • 23

2 Answers2

93

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 :

  1. First Execution :

    Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
    
  2. Second Execution :

    Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36
    
  3. 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
    
Tim Woocker
  • 1,632
  • 14
  • 28
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
23

You should use ChromeOptions from selenium.webdriver:

from selenium import webdriver

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"')
driver = webdriver.Chrome(chrome_options=chrome_options)

This should work.

Tim Woocker
  • 1,632
  • 14
  • 28
  • 1
    Thanks for the response. Your code is probably right, but I haven't been able to get it to work yet. Right now the command prompt opens and Chrome isn't launched, even when I add code for Chrome to open to a specific page. Any ideas? – theCrabNebula Mar 29 '18 at 23:13
  • 2
    @theCrabNebula Do you receive any error messages, if so what are they? I commonly see that this problem related to not referencing the filesystem location of the chromedriver executable properly. – Liquidgenius Jul 11 '18 at 19:17
  • 1
    'chrome_options' is deprecated. Use 'options' instead. – c10ud Feb 22 '21 at 05:17
  • Hi, sorry for my "noob" question but what's the point of changing UserAgent if a bot keep using the same iP address? In this case, wouldn't changing the UserAgent makes the Selenium bot more suspicious and prone to be blocked by website? – Upchanges Mar 01 '22 at 00:28