将焦点转移到另一个应用程序(它的第二个副本)上

时间:2018-12-05 17:26:56

标签: c#

我正在尝试使Internet Explorer集中精力,以便可以将文本输入到选定的文本框中。

我正在使用硒,并且Sendkeys函数不适用于我正在自动化的特定站点,因此我实现了一个虚拟键盘,该键盘可以键入但不能指定要键入的特定窗口。

因此,在调用打字功能之前,我需要先集中精力。一个警告是,为了使IE硒正常工作,我必须在启动Webdriver之前先打开IE的副本,因此当我执行以下代码时,它将弹出Internet Explorer的第一个过程

我花了一点时间才发现IEFrame是我需要的WindowClassName(我只是猜到了),但是我怎么能弄清Internetexplorer的第二个副本的类名是什么,这样我就不会集中错误的代码了。

   [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    [System.Runtime.InteropServices.DllImport("User32.dll")]
    public static extern bool ShowWindow(IntPtr handle, int nCmdShow);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    IntPtr hWnd = FindWindow("IEFrame", null); // this gives you the handle of the window you need.

    private void button1_Click(object sender, EventArgs e)
    {
        ShowWindow(hWnd, 9);
        SetForegroundWindow(hWnd);
    }

我还没有找到有关堆栈溢出或谷歌的答案,但它告诉我如何查找WindowClassName是什么,我尝试使用.exe进程名称,但也无法正常工作。

0 个答案:

没有答案