首页 文章

机器人框架关闭浏览器弹出窗口

提问于
浏览
0

我点击我的应用程序中的网页上的链接,弹出一个新窗口(我正在使用Internet Explorer 11,不允许使用任何其他浏览器) .

该窗口从代码中打开为html页面: href=fileName.html .

但是,没有任何Robot框架Selenium2Library关键字会关闭弹出的窗口,例如 Close Window ,根据文档说,"Closes the currently open pop-up window" . 但是,此命令将关闭主浏览器窗口而不是弹出的窗口 .

我也尝试使用 Get Window 命令的变体,如 Get Window TitlesGet Window Identifiers ,但它返回主浏览器窗口或没有窗口 .

有没有办法关闭弹出的窗口?

谢谢

EDIT:

这是窗口的html链接:

<a title="Birth Proof Code More Information" id="uef-help0" href="birthProofHelp.html" target="_blank" rel="help" data-uefid="uef-li-hlp" data-modal-disable="true">More Info</a>

这是窗口的源代码:

!DOCTYPE HTML>
<html>
    <head>
        <title>Birth Proof</title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>
    <body>
        A - ALLEGED<br>
        B - BIRTH/BAPTISMAL CERTIFICATE (BEFORE THE AGE OF 5)  / PREFERRED BIRTH PROOF<br>
        C - DEFINITION OF 'CONVINCING EVIDENCE' MET<br>
        F -  PREVIOUSLY ESTABLISHED BY THE SSA<br>
        Q - ESTABLISHED BASED ON EVIDENCE OTHER THAN THAT COVERED UNDER CODES 'B' AND 'C'<br>
    </body>
</html>

我尝试睡了60秒才能显示窗口,但仍然无法正常工作 .

窗户真的出现了 . 就像我说的那样, Close Window 关闭了主浏览器窗口而不是弹出的窗口 .

根据Robot文档, Get Window Titles 执行此操作:

返回当前浏览器实例已知的所有窗口的 Headers .

1 回答

  • 0

    如果所有其他方法都失败,则java.awt.robot是一个选项 . 使用Windows快捷方式关闭窗口:ctrl w .

    编辑:首先添加如何聚焦窗口(I 've been using this for a while and it' s为我工作,我想我原来是从here得到它,从@Avinash Jha回答):

    import com.sun.jna.Native;
        import com.sun.jna.platform.win32.WinDef.HWND;
        import com.sun.jna.win32.W32APIOptions;
    
        ...
    
        public interface User32 extends W32APIOptions {
            User32 instance = (User32) Native.loadLibrary("user32", User32.class, DEFAULT_OPTIONS);
            boolean ShowWindow(HWND hWnd, int nCmdShow);
            boolean SetForegroundWindow(HWND hWnd);
            HWND FindWindow(String title);
            int SW_SHOW = 1;
        }
    
        ...
    
        User32 user32 = User32.instance;
        HWND hWnd = user32.FindWindow("window title");
        user32.ShowWindow(hWnd, User32.SW_SHOW);
        user32.SetForegroundWindow(hWnd);
    

    然后机器人部分应该是这样的:

    import java.awt.*;
    import static java.awt.event.KeyEvent.*;
    
    ...
    
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_W);
    robot.keyRelease(KeyEvent.VK_W);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    

相关问题