Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to change Google Chrome user agent in Selenium?

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.

like image 339
theCrabNebula Avatar asked Mar 29 '18 21:03

theCrabNebula


People also ask

How do I change user agent in Chrome using Selenium?

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.

How do I change user agent manually?

To open them, click the settings menu and select “F12 Developer Tools” or just press F12 on your keyboard. The developer tools will open in a separate pane at the bottom of the window. Click the “Emulation” tab and choose a user agent from the “User agent string” box.


1 Answers

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 
like image 182
undetected Selenium Avatar answered Sep 19 '22 01:09

undetected Selenium