Selenium FireFoxDriver Profile changing after loading firefox?

折月煮酒 提交于 2019-12-01 12:37:33

问题


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?


回答1:


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()



回答2:


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");



回答3:


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

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