SWT Shell 内部的 Selenium WebDriver EDGE

Selenium WebDriver EDGE inside SWT Shell

提问人:Clonw 提问时间:11/13/2023 最后编辑:Clonw 更新时间:11/13/2023 访问量:27

问:

我有一个 Java SWT 应用程序(版本 1.6),我在其中使用 swt。用于显示嵌入式浏览器的浏览器。但是,swt.浏览器基于 IE,我无法更新 SWT 以将浏览器与 EDGE 一起使用。

为了解决此问题,我们创建了一个并行的 Selenium WebDriver 应用程序,用于启动 EDGE 类型的浏览器。在 SWT 应用程序中,我检索了 EDGE 的 PID,并使用 org.eclipse.swt.internal.win32.OS 库,我设法将 EDGE 窗口设置为 SWT shell 的子窗口。

在 EDGE 窗口中,我可以用鼠标单击,但它不会捕获关键事件。关键事件在 SWT shell 中捕获。关于为什么关键事件没有在 EDGE 窗口中被拾取的任何想法?

在 Shell 的 open() 方法中,我们使用以下 java 代码来定位 EDGE 窗口

import org.eclipse.swt.internal.win32.OS;

//...
                OS.ShowWindow(iWndMyWebDriver, OS.SW_HIDE);     
        
        OS.SetWindowLong(iWndMyWebDriver, OS.GWL_STYLE, OS.WS_CHILD & ~OS.WS_POPUP);
        
        
        OS.SetParent(iWndMyWebDriver, handle);
        
        int style = OS.GetWindowLongA(iWndMyWebDriver, OS.GWL_STYLE);

        style = style & ~OS.WS_SYSMENU;
        style = style & ~OS.WS_MAXIMIZEBOX;
        style = style & ~OS.WS_MINIMIZEBOX;
        style = style & ~OS.WS_CAPTION;

        OS.SetWindowLongA(iWndMyWebDriver, OS.GWL_STYLE, style);

        if (!isDisposed()) {
            OS.SetWindowPos(iWndMyWebDriver, 0, -1, -2, getBounds().width, 495, 4 + 32);
        }

        OS.BringWindowToTop(iWndMyWebDriver);

        RECT r = new RECT();
        r.left = 0;
        r.top = 0;

        OS.RedrawWindow(iWndMyWebDriver, r, 0, 256 + 512 + 1 + 2 + 128);

        OS.ShowWindow(iWndMyWebDriver, OS.SW_SHOW);

        OS.SetForegroundWindow(iWndMyWebDriver);
        
        if (!isVisible()) {
            setVisible(true);
            
            Pointer pHwnd = new Pointer(this.handle);

            User32.INSTANCE.ShowWindow(pHwnd, 9);

            User32.INSTANCE.SetForegroundWindow(pHwnd);

            UtilsOS.SetWindowAlwaysOnTop(this.handle, true);
            
            User32.INSTANCE.SetActiveWindow(pHwnd);
            
            setFocus();
    
            UtilsOS.SetWindowAlwaysOnTop(this.handle, false);

        }

编辑: 我添加了以下代码:

addKeyListener( new KeyListener() {
            
            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
                System.out.println( "key Released");
            }
            
            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                System.out.println( "Key pressed");
            }
        });

当我将焦点放在 EDGE 上并按下一个键时,两个事件被捕获,我希望这些事件进入浏览器。

selenium-webdriver web驱动程序 swt java-6

评论

0赞 greg-449 11/13/2023
Java 6 在 8 年前就结束了生命周期。为什么不能更新 SWT(和 Java)?所有这些东西都是 SWT 内部的,所以 SWT 开发人员可能是唯一可以回答这个问题的人,但他们不太可能对 SWT 的古老版本感兴趣。
0赞 Clonw 11/13/2023
@greg-449,这是公司的战术决定,做出决定不是我的责任,尽管作为开发人员,我的建议是更新。

答: 暂无答案