【发布时间】: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