3

I'm building a search bot and i want it to change from Desktop to Mobile

I tried to use profile.set_preferences but for some reason it wont change. It doesn't give compiling time error but it wont change the user agent. I also tried setting the desired capabilities but that didn't work either.

if count == 0:
    browser = webdriver.Firefox(executable_path="geckodriver.exe")
else:
    profile = webdriver.FirefoxProfile()
    profile.set_preference("general.useragent.override", "Mozilla/5.0 (Android 4.4; Tablet; rv:41.0) Gecko/41.0 Firefox/41.0")
    browser = webdriver.Firefox(profile)

I want it to search once as a regular browser and then to search as a mobile device but it just searches as a regular browser both times and i am sure that i increment count.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

7

A simple way to fake the User Agent would be using the FirefoxProfile() as follows :

from selenium import webdriver
from fake_useragent import UserAgent

useragent = UserAgent()
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", useragent.random)
driver = webdriver.Firefox(firefox_profile=profile, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("http://www.whatsmyua.info/")

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I just tried this and it still does the same. It still only opens up as a regular browser. – SuperSimplePimpleDimple Jan 19 '19 at 21:56
  • Is it possible that its a problem with the webdriver binary? I have been working with selenium webdrivers for over 5 months and last time i needed to swap user agents it was easy, i used desired_capabilities but then i was using chrome. Now with firefox webdriver it gives me errors or it doesnt run as expected. – SuperSimplePimpleDimple Jan 19 '19 at 22:00
  • @SuperSimplePimpleDimple Isn't your question about changing the UserAgent? Everytime the regular browser will be opened with a random UserAgent only. – undetected Selenium Jan 19 '19 at 22:00
  • Yes but it doesnt open with a random user agent. It opens up as a regular browser. – SuperSimplePimpleDimple Jan 19 '19 at 22:02
  • I want more specifically to change to a mobile user agent. Tablet if possible but a regular phone will be fine as well. Basically im making a bing bot and I need to switch from desktop to mobile in order to take advantage of the 150 free points on mobile. I did it without a problem on chrome webdriver but now i found out that i can make more money by swapping regions and in order to not get my accounts blacklisted i use a proxy and i change region from settings so i must use Firefox. I know im a scum for farming points but its free money and im in highschool and i need money for collage – SuperSimplePimpleDimple Jan 19 '19 at 22:08
  • @SuperSimplePimpleDimple Browser would be the regular browser but everytime the UserAgent will be different. – undetected Selenium Jan 19 '19 at 22:08
  • So even if it looks as a regular browser it is actually registered as a mobile device? – SuperSimplePimpleDimple Jan 19 '19 at 22:10
  • 1
    @SuperSimplePimpleDimple Can't comment about mobile device specific facts. But on each run the UserAgent will be different. That's it. – undetected Selenium Jan 19 '19 at 22:12