【问题标题】:IE Automation to get Address bar not workingIE 自动化使地址栏无法正常工作
【发布时间】:2013-01-24 06:42:42
【问题描述】:

有人能解释一下这个 sn-p 吗? 我正在使用 IE 9,有时它可以工作到点,有时不能,但不会更远。

IntPtr IEwindowHandle = GetForegroundWindow();
IntPtr childHandle = IntPtr.Zero;
childHandle = FindWindowEx(IEwindowHandle, IntPtr.Zero, "WorkerW", IntPtr.Zero);
if (childHandle != IntPtr.Zero)
{
    //get the handle to the address bar on IE
    childHandle = FindWindowEx(childHandle, IntPtr.Zero, "ReBarWindow32", IntPtr.Zero);
    if (childHandle != IntPtr.Zero)
    {
       // Usually it get until HERE  <---------
        // get a handle to comboBoxEx32
        childHandle = FindWindowEx(childHandle, IntPtr.Zero, "ComboBoxEx32", IntPtr.Zero);
        if (childHandle != IntPtr.Zero)
        {
            // get a handle to combo box
            childHandle = FindWindowEx(childHandle, IntPtr.Zero, "ComboBox", IntPtr.Zero);
            if (childHandle != IntPtr.Zero)
            {
                //get handle to edit
                childHandle = FindWindowEx(childHandle, IntPtr.Zero, "Edit", IntPtr.Zero);
                if (childHandle != IntPtr.Zero)
                {
                    // now to get the URL we need to get the Text - but first get the length of the URL
                    int length = SendMessage(childHandle, WM_GETTEXTLENGTH, 0, null);
                    length += 1;    // because the length returned above included 0
                    StringBuilder text = new StringBuilder(length); // need stringbuilder - not string
                    int hr = SendMessage(childHandle, WM_GETTEXT, length, text); // get the URL
                    string strURL = text.ToString();
                }
            }
        }
    }
}

还有 FindWindowEx 的声明

 [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);

这让我发疯......这应该有效!

【问题讨论】:

  • 您可以在这里使用我的答案中的代码,看看它是否工作得更好:stackoverflow.com/questions/5317642/…
  • 非常感谢 Simon 指出您的解决方案(我确信它会起作用,尽管我还没有测试过)。我还阅读了有关此线程的其他解决方案(并且因为已经在其他浏览器中使用了该方法)我采用了这种方法。

标签: c# automation internet-explorer-9 pinvoke


【解决方案1】:

@Simon 感谢您将我指向此线程!我最终使用了以下方法(这只是一个代码sn-p,重要的部分):

string url = string.Empty;
try
{
    DdeClient oDde = new DdeClient("IExplore", "WWW_GetWindowInfo");
    try
    {
        oDde.Connect();
        url = oDde.Request("1", int.MaxValue);
        oDde.Disconnect();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
catch (Exception ex)
{
    throw ex;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2019-01-11
    • 2016-09-23
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多