finefoot
finefoot

Reputation: 11224

How to read preferences, get value of profile setting from Selenium Firefox webdriver via geckodriver?

I can set a preference with set_preference, for example:

profile = selenium.webdriver.FirefoxProfile()
profile.set_preference('permissions.default.image', 2)

However, I can't figure out how I can read the current value of a setting?

Upvotes: 3

Views: 2435

Answers (2)

Todor Minakov
Todor Minakov

Reputation: 20047

If you're looking for a preference/setting, that is not exposed in selenium (it loads it from a file "webdriver_prefs.json", which has the ones it actually needs/can control), then you can get it from the about:config page itself.

DISCLAIMER - this approach uses Firefox js objects that may change in future versions - and thus, stop working.

The idea is - open "about:config", search for the key, and get its value. If you do that manually, you'll see this is not the regular html pages you usually work with - it's an xml full of namespaces etc. Yet the data is stored in a js object view.

So the flow is - open the config page, and do all the work through JS:

from selenium import webdriver


def get_preference(name):
    """ Runs a JS that a) sets the value of the searched-for setting to the name argument,
        b) looks for the value of the first element in the "table" below.
        Thus the name better be exact, and you'd better be looking for the 1st match :) """

    global driver
    value = driver.execute_script("""
        document.getElementById("textbox").value = arguments[0];
        FilterPrefs();
        view.selection.currentIndex = 0;
        var value = view.getCellText(0, {id:"valueCol"});
        return value;
    """, name)
    return value


if __name__ == '__main__':
    try:
        ff_profile = webdriver.FirefoxProfile()
        ff_profile.set_preference("general.warnOnAboutConfig", False)  # there's a nasty warning opening about:config (another setting not present in the selenium preferences ;)

        driver = webdriver.Firefox(firefox_profile=ff_profile)
        driver.get('about:config')
        print(get_preference('devtools.jsonview.enabled'))
    finally:
        driver.quit()

Upvotes: 1

Todor Minakov
Todor Minakov

Reputation: 20047

They are in the default_preferences instance attribute:

print(profile.default_preferences)

Looking at the code, it's a dictionary.

Upvotes: 3

Related Questions