2

I'm using python+selenium, with profile that has addons. On startup it shows them all momentarily, but then they are concealed. They exist, they are not disabled, but invisible and don't work. I can disable and enable it, then it appears on the taskbar and is functional. screenshot

When invoking firefox manually with the profile, it works.

Here's what printed to the log.

1596129664500   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofileO34n0s"
JavaScript error: resource:///modules/sessionstore/SessionStore.jsm, line 1325: uncaught exception: 2147746065
JavaScript error: resource://gre/modules/ExtensionContent.jsm, line 554: TypeError: Argument 1 of PrecompiledScript.executeInGlobal is not an object.
1596129672037   Marionette  INFO    Listening on port 41285
1596129672136   Marionette  WARN    TLS certificate errors will be ignored for this session
JavaScript error: undefined, line 14: Error: An unexpected error occurred
JavaScript error: moz-extension://45aaa1ae-14fe-4a8f-841d-6a9416fd5d09/lib/picture_in_picture_overrides.js, line 15: Error: Incorrect argument types for pictureInPictureParent.setOverrides.
1596129683512   Marionette  INFO    Stopped listening on port 41285

Can it be because of these errors?

The code itself is of no interest at all:

#!/usr/bin/env python

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

profile_path='./profile'

opts=Options()
opts.profile=profile_path
driver=Firefox(options=opts)

breakpoint()

Versions are probably more important:

  • Mozilla Firefox 68.9.0esr
  • geckodriver 0.26.0 (e9783a644016 2019-10-10 13:38 +0000)

I'm creating an empty directory, then running firefox --new-instance --profile ./profile, then installing addon manually into the profile. But here there is that profile

aikipooh
  • 137
  • 1
  • 19
  • Can you please provide what code sample you have written till now so that people can help you. – Swaroop Humane Jul 30 '20 at 17:34
  • @Pythonologist, yep, moment, will make a bare minimum. – aikipooh Jul 30 '20 at 17:45
  • Can you please provide your profile, or at the very least the relevant parts of it? – alexisdevarennes Aug 03 '20 at 13:59
  • @alexisdevarennes, well, it was fresh one with one addon manually installed, but I'll try to send one tomorrow. – aikipooh Aug 03 '20 at 20:50
  • @alexisdevarennes I've added a link to a profile. But I still think that it's something about some settings, I just don't know which. OS-wide or something about browser. I was trying to remember, but I think I never used profiles with addons with selenium before, so cannot say whether it worked on my system at all. – aikipooh Aug 04 '20 at 06:07

1 Answers1

6

In your profile there is a file: prefs.js which contains a line user_pref("extensions.lastAppBuildId", "20200707180101"); this line might be at fault for the disabled addons. You could therefore test to decrease this number by 1 or remove the whole line (untested).

profile.set_preference("extensions.lastAppBuildId", "<apppID> -1 ")

Full example code:

from selenium.webdriver import FirefoxProfile
from selenium import webdriver
path = '%APPDATA%\Mozilla\Firefox\Profiles\azk4wioue.default' #path to your profile
profile = FirefoxProfile(path) 
profile.set_preference("extensions.lastAppBuildId", "<apppID> -1 ")

driver = webdriver.Firefox(profile)

Example to use an existing firefox profile:

# go to the the following folder %APPDATA%\Mozilla\Firefox\Profiles\
# there the firefox profiles should be stored, the default one ending with .default
# now provide the profile to the driver like this:
profile = FirefoxProfile('%APPDATA%\Mozilla\Firefox\Profiles\azk4wioue.default') 
driver = webdriver.Firefox(firefox_profile=profile)

Alternatively install the addon clean on each run through a temporary profile.

# go to https://addons.mozilla.org and search for the plugin you want, e.g.:https://addons.mozilla.org/en-US/firefox/addon/ublock-origin/
# rightclick on the button "add to firefox"
# download the target file to a folder of your choice

# then include the addon like this:
driver.install_addon('/Users/someuser/app/extension.xpi', temporary=True)

Alternatively 2, you can try setting the extension this way:

from selenium.webdriver import FirefoxProfile
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.add_extension(extension='/Users/someuser/app/extension.xpi')

driver = webdriver.Firefox(profile)

If the addons are there after loading the profile but disabled, you can also try this:

def newTab(fx, url="about:blank"):
    wnd = fx.execute(selenium.webdriver.common.action_chains.Command.NEW_WINDOW)
    handle = wnd["value"]["handle"]
    fx.switch_to.window(handle)
    fx.get(url) # changes handle
    return fx.current_window_handle

def retoggleAllTheAddons(fx):
    initialHandlesCount = len(fx.window_handles)
    addonsTabHandle = newTab(fx, "about:addons")
    fx.execute_script("""
        let hb = document.getElementById("html-view-browser");
        let al = hb.contentWindow.window.document.getElementsByTagName("addon-list")[0];
        let cards = al.getElementsByTagName("addon-card");
        for(let card of cards){
            card.addon.disable();
            card.addon.enable();
        }
    """)
    if len(fx.window_handles) != 1:
        fx.switch_to.window(addonsTabHandle)
        fx.close()
Andreas
  • 8,694
  • 3
  • 14
  • 38
  • I was considering this, but since that addon requires configuration (entering a key) I wanted to evade complications and just use the precooked profile… – aikipooh Aug 04 '20 at 05:56
  • Ah, makes sense. I updated my answer and added a way to use existing profiles. – Andreas Aug 04 '20 at 08:17
  • No, the same, the behaviour doesn't depend on how I pass the profile, via opts or via profile argument. – aikipooh Aug 04 '20 at 08:38
  • Updated my answer. Seems like this is a problem in geckodriver. – Andreas Aug 04 '20 at 10:04
  • Oh! Probably that's magic I'm looking for! Will give it a try later today or tomorrow. Thank you! – aikipooh Aug 04 '20 at 14:15