Change browser preferences in runtime?

倖福魔咒の 提交于 2019-12-06 11:58:32

问题


Can we change browser preferences in runtime?

Is there any possibility to changes the browser preferences set before launching the browser during execution programmatically?

Example:

I have set the following preferences before launching the driver

firefoxProfile.setPreference("pdfjs.disabled", true);
firefoxProfile.setPreference("plugin.scan.plid.all", false);
firefoxProfile.setPreference("plugin.scan.Acrobat", "99.0");

I want to change the preference to :

firefoxProfile.setPreference("pdfjs.disabled", **false**);
firefoxProfile.setPreference("plugin.scan.plid.all", **true**);
firefoxProfile.setPreference("plugin.scan.Acrobat", "99.0");

Please help!!

Thanks


回答1:


It possible to change the settings at run-time using about:config UI. Below code demonstrates how to do the same

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference("general.warnOnAboutConfig", False)

driver = webdriver.Firefox(firefox_profile=profile)

driver.get("about:config")

def set_bool_preferce(name, value):
    value = 'true' if value else 'false';

    driver.execute_script("""
        document.getElementById("textbox").value = arguments[0];
        FilterPrefs();
        view.selection.currentIndex = 0;

        if (view.rowCount == 1) {
           current_value = view.getCellText(0, {id:"valueCol"});
           if (current_value != arguments[1]) {
               ModifySelected();
           }
        } 
    """, name, value)


def set_string_preferce(name, value):

    modified = driver.execute_script("""
        document.getElementById("textbox").value = arguments[0];
        FilterPrefs();
        view.selection.currentIndex = 0;

        if (view.rowCount == 1) {
           current_value = view.getCellText(0, {id:"valueCol"});
           if (current_value != arguments[1]) {
               ModifySelected();
               return true;
           }
        } 

        return false;
    """, name, value)

    if modified is None or modified is True:
        alert = driver.switch_to.alert
        alert.send_keys(value)
        alert.accept()


set_bool_preferce("pdfjs.disabled", True)
set_string_preferce("plugin.disable_full_page_plugin_for_types", "application/pdf,application/pdf2")

driver.quit()


来源:https://stackoverflow.com/questions/46470473/change-browser-preferences-in-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!