ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("default");//using firefox default profile
ffprofile.setPreference("permissions.default.image", 2); // this make ff to block web page images
WebDriver ff = new FirefoxDriver(ffprofile); // executing firefox with specified profile
ff.navigate().to("www.google.com"); // loading web page
//codes for changing image blocking ???????????
How can I change the image blocking after loading some web pages?
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.
来源:https://stackoverflow.com/questions/23516064/selenium-firefoxdriver-profile-changing-after-loading-firefox