Selenium FireFoxDriver在加载firefox后更改配置文件?

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 ??????????? 

如何在加载某些网页后更改图像阻止?

可以通过开发工具栏CLI修改飞行中的首选项,但它可能会比加载图像带来更高的开销。 这是Python示例:

 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains, Keys ff = webdriver.Firefox() ff.get('http//') 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() 

从命令行firefox.exe -p运行firefox

之后创建新的配置文件,设置neccessery设置并始终调用此配置文件。

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

由于开发工具栏CLI对我不起作用(因为组合键不能打开CLI),所以我设法更改正在运行的firefox实例的配置文件设置:

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

它是C#但Java中的转换不应该太难。 想法是about:config选项卡声明了许多允许更改配置文件设置的Javascript对象,因此我们只需要继续该页面并执行一些JS代码。

Interesting Posts