如何隐藏 Firefox 窗口(Selenium WebDriver)?

2022-01-16 00:00:00 python selenium webdriver firefox

问题描述

当我同时执行多个测试时,我不想让 Firefox 浏览器窗口保持可见.我可以使用 selenium.minimizeWindow() 将其最小化,但我不想这样做.

When I execute multiple test simultaneously, i don't want to keep Firefox browser window visible.. I can minimize it using selenium.minimizeWindow() but I don't want to do it.

有什么方法可以隐藏 Firefox 窗口吗?我正在使用 FireFox WebDriver.

Is there any way to hide Firefox window? I am using FireFox WebDriver.


解决方案

最后我找到了解决方案,适用于那些使用 windows Machine 使用任何方法运行测试的人.好吧,实现不是在 Java 中,但你可以很容易地做到这一点.

Finally I found the solution for those who are using windows Machine for running the Tests using any method. Well, implementation is not in Java, but you can do it very easily.

使用 AutoIt 工具.它具有处理窗口的所有能力.它是一个免费工具.

Use AutoIt tool. It has all the capability to handle windows. It is a free tool.

  1. 安装 AutoIt:http://www.autoitscript.com/site/autoit/downloads/

打开编辑器并编写以下代码用于隐藏任何窗口.

Open the Editor and write below code for Hiding any window.

AutoItSetOption("WinTitleMatchMode", 2)
WinSetState("Title Of Your Window", "", @SW_HIDE) 

  • 要取消隐藏,可以使用下面的代码.

  • To Unhide it, you can use below line of code.

    AutoItSetOption("WinTitleMatchMode", 2)
    WinSetState("Title Of Your Window", "", @SW_SHOW)
    

    WinTitleMatchMode 有不同的选项可用于匹配 Windows 标题.

    WinTitleMatchMode has different options which can be used to match Windows title.

    1 = Match the title from the start (default)`
    2 = Match any substring in the title
    3 = Exact title match
    4 = Advanced mode, see Window Titles & Text (Advanced)
    

  • 所以,我所做的是:我创建了一个小程序的 .exe 文件,并将参数作为命令行参数传递,如下所示.

    So, what I've done is: I have created an .exe file of a small program and passed a parameter as a command line argument as below.

    Runtime.getRuntime().exec("C:/Diiinnovation/HideNSeek.exe 0 "" + "Mozilla Firefox" + """);
    

    HideNSeek.exe - 我有下面的 AutoIt 代码:

    in HideNSeek.exe - I have below AutoIt Code:

    AutoItSetOption("WinTitleMatchMode", 1) 
    
    if $CmdLine[0] > 0 Then
        if $CmdLine[1] == 0 Then
            WinSetState($CmdLine[2], "", @SW_HIDE)    
        ElseIf $CmdLine[1] == 1 Then
            WinSetState($CmdLine[2], "", @SW_SHOW)          
        Else    
        EndIf   
    EndIf
    

    $CmdLine[] 是一个数组,它将包含所有命令行参数...

    $CmdLine[] is an array, which will have all command line parameters...

    $CmdLine[0] = number of Parameter
    $CmdLine[1] = 1st Parameter after Exe Name 
    ...
    

    如果窗口标题中有空格,则必须使用双引号将其作为命令行参数传递,如上.

    If there is any space in the Window Title, then you have to use double quotes to pass it as a command line parameter like above.

    下面的代码行将执行 AutoIt exe,如果我在第一个参数中传递 '0' 那么它将隐藏窗口,如果我将传递 '1' 然后它将取消隐藏与标题匹配的窗口.

    Below Line of code will execute AutoIt exe and if I pass '0' in 1st parameter then it will hide the window and if I will pass '1' then it will unhide windows matching the title.

    Runtime.getRuntime().exec("C:/Diiinnovation/HideNSeek.exe 0 "" + "Mozilla Firefox" + """);
    

    我希望这会对你有所帮助.谢谢!

    I hope this will help you. Thanks!

    相关文章