Python 木偶客户端不会关闭最后一个窗口



这是一个小程序,似乎没有关闭最后一个选项卡。

从 marionette_driver.木偶进口木偶

            client = Marionette("localhost", socket_timeout=30, port=proc_port)
            client.start_session()
            client.set_window_size(1024,768)
            client.close()

这不会关闭最后一个选项卡,但如果有多个选项卡,它将关闭一个选项卡。

调用 client.quit() 将抛出错误。

如何从 python 木偶客户端关闭最后一个选项卡/窗口?

我会自己回答这个问题。在很多崩溃之后,我搜索了崩溃字符串,它引导我在 Mozilla 开发网络上找到这个文件 mariontte.py

浏览代码,我看到了这些行

@do_process_check
def quit(self, in_app=False):
    """Terminate the currently running instance.
    This command will delete the active marionette session. It also allows
    manipulation of eg. the profile data while the application is not running.
    To start the application again, start_session() has to be called.
    :param in_app: If True, marionette will cause a quit from within the
                   browser. Otherwise the browser will be quit immediately
                   by killing the process.
    """
    if not self.instance:
        raise errors.MarionetteException("quit() can only be called "
                                         "on Gecko instances launched by Marionette")

我一直在崩溃quit() can only be called on Gecko instanced launched by Marionette

然后我环顾四周,看到强制退出选项,所以我尝试了一下,它似乎杀死了最后一个 Firefox 窗口。

也许将来 Firefox 团队可以解决这个问题,或者我可能错误地使用了 API。

如果打开了 1 个以上的选项卡,则可以通过切换到该窗口句柄并在该窗口句柄上调用 client.close() 来关闭它。

如果您只打开了一个选项卡,则不再有效,对我来说,我必须打电话

    client._send_message("quitApplication", {"flags": ["eForceQuit"]})

以关闭最后一个窗口并退出。

最新更新