Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium firefox - add_extension not working

Trying to add uBlock to a browser session but it's not working.

import selenium
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.options import Options as options


def establish_browser(type, hide):
    browser = ''
    if type == 'firefox':
        ops = options()
        ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
        profile = selenium.webdriver.FirefoxProfile()
        profile.add_extension(extension='[email protected]')
        browser = selenium.webdriver.Firefox(firefox_profile=profile, executable_path='geckodriver.exe', options=ops, firefox_binary=FirefoxBinary('C:/Program Files/Mozilla Firefox/firefox.exe'))
    return browser

browser = establish_browser('firefox', False)

How should this be changed so uBlock works?

UPDATE

The chrome version appears to be working …

if type == 'chrome':
    from selenium.webdriver.chrome.options import Options as options
    ops = options()
    ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
    ops.add_extension("ublock.crx")
    browser = selenium.webdriver.Chrome(executable_path='chromedriver.exe', options=ops, desired_capabilities={'binary_location': 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'})

is Firefox depreciated?

like image 546
Rhys Avatar asked Nov 17 '25 16:11

Rhys


1 Answers

Since, for some reason, chrome's add_extension works but firefox's add_extension does not work (currently) … here is my workaround for adding extensions to firefox.

  1. create a new firefox profile via right click windows start button > run > firefox.exe -P
  2. Then add whatever extensions you want, ublock, adblock plus etc
  3. call your profile folder with

profile = selenium.webdriver.FirefoxProfile("C:/test")

browser = selenium.webdriver.Firefox(firefox_profile=profile, options=ops)

Apparently profile.add_extension() is not a must have for this workaround

UPDATE! - added chrome profile

For symmetry purposes I have updated the chrome example code to use the chrome profile instead of calling the .crx directly.

  1. install extensions onto chrome's default profile.
  2. navigate to C:\Users\User\AppData\Local\Google\Chrome or where-ever chromes User Data folder is located. Call this folder directly (absolute path) or rename it and call the relative path. I have renamed it to chrome_profile:

    ops = options()
    ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
    ops.add_argument('user-data-dir=chrome_profile')
    ops.add_argument('--profile-directory=Default')
    ops.add_argument("--incognito")
    browser = selenium.webdriver.Chrome(executable_path='chromedriver.exe', options=ops, desired_capabilities={'binary_location': 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'})
    
like image 66
Rhys Avatar answered Nov 20 '25 07:11

Rhys



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!