Selenium FireFoxDriver Profile changing after loading firefox?

空扰寡人 提交于 2019-12-01 14:47:38

It is possible to modify preferences in flight via dev toolbar CLI but it may introduce higher overhead than loading images. Here is Python example:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains, Keys

ff = webdriver.Firefox()
ff.get('http//<URL>')

ac = ActionChains(ff)
# SHIFT+F2 opens dev toolbar
ac.key_down(Keys.SHIFT).send_keys(Keys.F2).key_up(Keys.SHIFT).perform()
# command to disable images
ac.send_keys('pref set permissions.default.image 2').perform()
ac.send_keys(Keys.ENTER).perform()
# command to disable flash
ac.send_keys('pref set plugin.state.flash 0').perform()
ac.send_keys(Keys.ENTER).perform()
# disable dev toolbar
ac.key_down(Keys.SHIFT).send_keys(Keys.F2).key_up(Keys.SHIFT).perform()
ac.key_down(Keys.SHIFT).send_keys(Keys.F2).key_up(Keys.SHIFT).perform()
# reload the page to confirm there are no images or flash
ff.refresh()

Run the firefox from command line firefox.exe -p

After that create a new profile, set the neccessery settings and always call this profile.

FirefoxProfile ffprofile = profile.getProfile("profileName");

Since the dev toolbar CLI does not work for me (since the key combination does not open the CLI) here is how I managed to change profile settings of a running firefox instance:

        IWebDriver driver = //your instance
        driver.Navigate().GoToUrl("about:config");
        driver.FindElement(By.Id("warningButton")).Click();
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

        js.ExecuteScript("window.Services.prefs.setIntPref('permissions.default.image', " + 2 + ")");

It's C# but the conversion in Java should not be too hard. The idea is that the about:config tab declares a lot of Javascript object allowing to change the profile settings, so we just have to go on that page and to execute some JS code.

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