0

The user agent is showing on the address bar like this http://user-agent=mozilla/5.0%20(Windows%20NT%2010.0;%20Win64;%20x64)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/87.0.4280.88%20Safari/537.36. Picture to illustrate the issue:

Picture showing the problem

from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
import time

option = Options()
option.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
option.set_preference("browser.link.open_newwindow", 3)
option.set_preference("browser.link.open_newwindow.restriction", 0)
browser = webdriver.Firefox(options=option)

How can I change the user agent of Firefox with Selenium through the option add_argument?

J. M. Arnold
  • 6,261
  • 3
  • 20
  • 38

2 Answers2

1
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "whatever you want")
driver = webdriver.Firefox(profile)

Add argument adds commandline arguments for the firefox or Chrome binary. Firefox doenst have any user agent commandline option

PDHide
  • 18,113
  • 2
  • 31
  • 46
0

Warning: FirefoxProfile is DEPRICATED

The normal method of using FirefoxProfile is now depricated and you must use Options instead.

How to change FireFox User Agent using Options

Simply do the same as FirefoxProfile but with Options object instead like so:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
agent = " Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
options.set_preference("general.useragent.override", agent)

driver = webdriver.Firefox(options=options)

Using random User Agent from git

To use a random user agent you can just pull from this git list of useragents at random (requires pip install requests):

import requests,random
agents = requests.get("https://gist.githubusercontent.com/pzb/b4b6f57144aea7827ae4/raw/cf847b76a142955b1410c8bcef3aabe221a63db1/user-agents.txt").text.split('\n')
agent = random.choice(agents)

Using random user agent from pip package

There is a helpful package called random-user-agent by installing through pip

pip install random-user-agent

Then to get a random agent:

from fake_useragent import UserAgent
agent = UserAgent().random

Full Example

Here is a full example of setting a random user agent for selenium using options:

from fake_useragent import UserAgent
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference("general.useragent.override", UserAgent().random)

driver = webdriver.Firefox(options=options)

Cheers

Tsangares
  • 780
  • 1
  • 9
  • 27