【问题标题】:How to automatically close firebug tab when launching selenium firefox webdriver with firebug?使用firebug启动selenium firefox webdriver时如何自动关闭firebug选项卡?
【发布时间】:2017-01-27 09:47:49
【问题描述】:

我在启动 firefox 驱动程序时包含了 firebug,它在最新的 selenium web driver 3.0 上运行良好,但同时每次启动浏览器时它也会打开新的 firebug 选项卡。

正如代码所说,我已经包含了 firebug 文件并在创建的配置文件中添加了这个扩展。有什么方法可以在启动浏览器后自动关闭 firebug 选项卡?如果没有自动方法,那么我需要使用调整来关闭名为“Firebug”的窗口,对吗?

代码:

File file = new File("./firebug-2.0.17-fx.xpi");
System.setProperty("webdriver.gecko.driver", config.getStringProperty("geckodriver.path"));
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability("marionette", true);
webDriver = new FirefoxDriver(capabilities);

【问题讨论】:

标签: selenium selenium-webdriver selenium-firefoxdriver


【解决方案1】:

我一直在寻找自动关闭 Firebug 窗口,但我认为它不可能,所以在启动具有 firebug 功能的 firebox 浏览器后发布处理打开的窗口的答案,不幸的是,由于这个问题,您需要处理额外的代码行 :)

下面的解决方案它工作正常,只需像下面这样使用它:

工作代码:

File file = new File("./firebug-2.0.17-fx.xpi");
System.setProperty("webdriver.gecko.driver", config.getStringProperty("geckodriver.path"));
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability("marionette", true);
webDriver = new FirefoxDriver(capabilities);

// Close the firebug window
Thread.sleep(4000); // Firebug window takes time to open it
Set <String> windows = webDriver.getWindowHandles();
String mainwindow = webDriver.getWindowHandle();

for (String handle: windows) {
    webDriver.switchTo().window(handle);
    if (!handle.equals(mainwindow)) {
        webDriver.close();
    }
}
webDriver.switchTo().window(mainwindow);

【讨论】:

    【解决方案2】:

    您可以在您的 porfile 中将“showFirstRunPage”标志设置为“false”。

    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("extensions.firebug.showFirstRunPage", false);
    

    这里是您可以设置的所有 firebug 首选项的链接。 Firebug Preferences

    另一种解决方案是将“currentVersion”设置为这样的大数字。

    profile.setPreference("extensions.firebug.currentVersion", "999");
    

    我更喜欢第一个;)

    【讨论】:

    • 非常棒!也请接受这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    相关资源
    最近更新 更多