使用 firebug 启动 selenium firefox webdriver 时如何自动关闭 firebug 选项卡?

How to automatically close firebug tab when launching selenium firefox webdriver with firebug?

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

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

代码:

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

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

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

工作代码:

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

您可以在您的文件中将 "showFirstRunPage" 标志设置为 "false"。

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

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

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

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

我更喜欢第一个;)