【问题标题】:C# IE11 Automation - Cannot Connect To Open IE WindowC# IE11 自动化 - 无法连接到打开 IE 窗口
【发布时间】:2017-10-07 02:16:34
【问题描述】:

我正在尝试连接到已打开的 Internet Explorer 窗口。连接后,我需要将一些击键(通过 SendKeys)发送到 IE 窗口进行一些处理。我有下面的代码,直到 SendKeys 命令。它找到标题为“图形数据库”的 IE 窗口。当它点击“SendKeys.Send("{TAB}");”我收到错误“发生'System.NullReferenceException'类型的未处理异常”。

附加信息:我还收到有关 NullReferenceException 错误的以下信息。奇怪的是,如果我编写代码以打开一个新的 IE 窗口,然后使用 SendKeys 它工作正常。连接到现有窗口似乎会导致此问题。

SendKeys 无法在此应用程序中运行,因为该应用程序未处理 Windows 消息。更改应用程序以处理消息,或使用 SendKeys.SendWait 方法。

谁能帮我弄清楚如何解决这个问题?

安迪

InternetExplorer IE = null;

// Get all browser objects
ShellWindows allBrowsers = new ShellWindows();
if (allBrowsers.Count == 0)
{
    throw new Exception("Cannot find IE");
}

// Attach to IE program process
foreach (InternetExplorer browser in allBrowsers)
{                           
    if (browser.LocationName == "Graphics Database")
    {
        MessageBox.Show ("Found IE browser '" + browser.LocationName + "'");
        IE = (InternetExplorer)browser;
    }
}

IE.Visible = true;
System.Threading.Thread.Sleep(2000);

SendKeys.Send("{TAB}");
SendKeys.Send("G1007");
SendKeys.Send("{ENTER}");

【问题讨论】:

  • 堆栈跟踪或....
  • 我发现更多的问题是 IE.Visible = true; 不起作用。如果我在 IE.Visible = true 之前和之后使用 MessageBox 暂停代码并手动单击“图形数据库”窗口以使其处于活动状态并位于前面,则以下代码将按预期工作。
  • 我能够解决这个问题。我永远无法让 IE.Visible = true 工作。这在我的代码中似乎毫无意义。我不得不使用 SetForegroundWindow() 将焦点设置到 IE 窗口。

标签: c# automation internet-explorer-11


【解决方案1】:

我能够解决这个问题。我永远无法让 IE.Visible = true 工作。这在我的代码中似乎没有任何作用。我不得不使用 SetForegroundWindow() 将焦点设置到 IE 窗口。

// Find the IE window 
int hWnd = FindWindow(null, "Graphics Database - Internet Explorer"); 

if (hWnd > 0) // The IE window was found. 
{ 
    // Bring the IE window to the front. 
    SetForegroundWindow(hWnd);

这个网站极大地帮助了我让 SetForegroundWindow() 正常工作。

http://forums.codeguru.com/showthread.php?460402-C-General-How-do-I-activate-an-external-Window

【讨论】:

    【解决方案2】:

    安迪请耐心等待,因为这会很长。首先,您将要查看 mshtml 文档和 Dom。 https://msdn.microsoft.com/en-us/library/aa741314(v=vs.85).aspx我不知道为什么自动化如此复杂,但确实如此。 UIautomation 类非常适用于 Windows 应用程序,但对于 IE 来说,我找不到任何东西。其他人会指向第三方,例如 waitn 和 selenium。 Waitn 似乎不再受支持,并且 selenium 不会让您获取打开的 IE 浏览器。我最近走上了这条路,因为我希望能够创建一个应用程序来存储我的网络密码并自动填写它们,因为由于安全限制,我无法在浏览器中保存我的用户名和密码。我在这里有一个例子,希望它有所帮助。首先打开 IE 并导航到http://aavtrain.com/index.asp。然后有一个引用 mshtml 和 shdocvw 的控制台项目。这是下面的代码。它获取窗口,然后查找用户名、密码和提交的元素。然后填充用户名和密码并单击提交按钮。我没有登录这个网站,所以它不会让你登录。我一直在使用它进行测试。我遇到的问题是带有 javascript 登录表单的网站。如果您进一步了解此信息,请回帖,因为我仍在尝试改进这些概念并创建可重用的东西。

            SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
    
            Console.WriteLine("Starting Search\n\n\n");
    
            foreach (SHDocVw.InternetExplorer ie in shellWindows)
            {
                if (ie.LocationURL.Contains("aavtrain"))
                {
                    Console.WriteLine(ie.LocationURL);
                    Console.WriteLine("\n\n\n\n");
                    Console.WriteLine("FOUND!\n");
    
                    mshtml.HTMLDocument document = ie.Document;
                    mshtml.IHTMLElementCollection elCol = document.getElementsByName("user_name");
                    mshtml.IHTMLElementCollection elCol2 = document.getElementsByName("password");
                    mshtml.IHTMLElementCollection elCol3 = document.getElementsByName("Submit");
    
                    Console.WriteLine("AutofillPassword");
    
    
                    foreach (mshtml.IHTMLInputElement i in elCol)
                    {
                        i.defaultValue = "John";
                    }
    
                    foreach (mshtml.IHTMLInputElement i in elCol2)
                    {
                        i.defaultValue = "Password";
                    }
    
                    Console.WriteLine("Will Click Button in 2 seconds");
                    Thread.Sleep(2000);
    
                    foreach (mshtml.HTMLInputButtonElement i in elCol3)
                    {
    
                        i.click();
                    }
    
    
                }
            }
    
            Console.WriteLine("Finished");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 2011-08-04
      • 1970-01-01
      相关资源
      最近更新 更多