2

我们可以在运行时更改浏览器首选项吗?

在以编程方式执行期间启动浏览器之前是否有可能更改浏览器首选项设置?

例子:

我在启动驱动程序之前设置了以下首选项

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

我想将首选项更改为:

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

请帮忙!!

谢谢

4

1 回答 1

1

可以使用about:configUI 在运行时更改设置。下面的代码演示了如何做同样的事情

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()
于 2017-09-28T21:08:48.727 回答