3

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?

2
  • 1
    Did you create an issue about this on geckodriver issue tracker ? Commented Aug 18, 2019 at 10:37
  • I might do that actually, I'll look into it tomorrow Commented Oct 12, 2019 at 17:36

2 Answers 2

3

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'})
    
Sign up to request clarification or add additional context in comments.

Comments

0

To add to @Rhys' solution, an easier approach might be the following option from the official documentation which works as expected:

driver = webdriver.Firefox('path/to/executable')
driver.install_addon('~/path/to/addon.xpi')

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
Agreed, that's why the necessary code is included below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.