【问题标题】:Selenium FireFoxDriver Profile changing after loading firefox?Selenium FireFoxDriver 配置文件在加载 Firefox 后更改?
【发布时间】:2014-06-24 07:52:14
【问题描述】:
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 ???????????

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

【问题讨论】:

    标签: java image selenium blocking selenium-firefoxdriver


    【解决方案1】:

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

    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】:

      由于开发工具栏 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 代码。

      【讨论】:

        【解决方案3】:

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

        然后创建一个新配置文件,设置必要的设置并始终调用此配置文件。

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

        【讨论】:

        • 我可以成功加载任何 Firefox 配置文件。但我想在 Firefox 加载后更改配置文件设置。用户可以通过在 ff 地址栏中输入“about:config”来更改任何配置文件设置。我想以编程方式更改它。
        • 这是不可能的。配置文件是在创建 Firefox 实例之前创建的,不能更改。
        • 是的。 Firefox 加载后无法更改“配置文件”,但我想更改“配置文件设置”。请尝试通过在地址栏中输入“about:config”来更改配置文件。然后您可以更改任何首选项,例如“permission.default.images”以阻止图像。
        • 您好 khostro,您是否成功地以编程方式更新配置文件设置 firefox 加载后?
        猜你喜欢
        • 2013-02-14
        • 2016-03-23
        • 1970-01-01
        • 2017-12-02
        • 2018-06-21
        • 2020-08-20
        • 1970-01-01
        • 2018-10-23
        相关资源
        最近更新 更多