【问题标题】:How to get the URL from the focused Internet Explorer browser window如何从焦点集中的 Internet Explorer 浏览器窗口获取 URL
【发布时间】:2019-11-01 11:36:58
【问题描述】:

我有一个键盘钩子实现,可以在给定条件下更改某些文本的输出。要确定如何格式化输出,我需要能够查看哪个窗口处于焦点位置,如果 Internet Explorer 处于焦点位置,我需要能够确定在该特定选项卡上打开了哪个 URL。

我一直在使用 Simon 在以下帖子中发布的代码: Retrieve current URL from C# windows forms application

Process[] localByName = Process.GetProcessesByName("iexplore");


if((Keys)vkCode == Keys.LShiftKey)
{
    return CallNextHookEx(_hookID, nCode, wParam, lParam);
}


foreach (Process process in Process.GetProcessesByName("iexplore"))
{
    string url = GetInternetExplorerUrl(process);
    if (url == null)
        continue;

    Console.WriteLine("IE Url for '" + process.MainWindowTitle + "' is " + url);
}

由于我正在运行 Internet Explorer(并为此打开网页),我希望/期待看到带有 URL 的某种输出,显示 URL 已打开。我没有将 URL 写入控制台,而是一无所获。在我尝试使用GetProcessesByName 的情况下,我只得到以下输出,System.Diagnostics.Process[]

【问题讨论】:

  • 不要引用我的话,但我似乎记得从外部程序读取 url 被故意阻止。即使在表单中嵌入 active-x 控件时,也很难做到。
  • 使用哪个 IE 版本?如果我在我的 IE 版本 (11) 上使用 Inspect 检查 IE 层次结构,我发现这段代码是错误的...
  • 所有需要检测的URL暂时都在IE11中。

标签: c# webautomation


【解决方案1】:

要从 Internet Explorer 选项卡中获取所有 URL,您可以执行以下操作:

1.添加对“Microsoft Internet Controls”的引用

2. 添加以下代码以获取 URL:

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

List<string> urls = shellWindows
    .Cast<SHDocVw.InternetExplorer>()
    .Where(ie => System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower() == "iexplore")
    .Select(ie => ie.LocationURL)
    .ToList();

foreach (string url in urls)
{
    Console.WriteLine(url);
}

【讨论】:

  • 谢谢!这非常适合在 IE 中打开所有 URL(至少在 IE11 中),但我真正需要做的是确定这些 URL 之一是否在焦点窗口中。我最初的想法是尝试将这些 URL 绑定到进程 ID,但我还不确定如何去做。
【解决方案2】:

这个解决方案是一种很老套的方法。

主要思想

  1. 发送 ALT+D 到浏览器以选择 URL 文本
  2. 发送CTRL+C复制网址

需要考虑的一些事项

  1. 要向窗口发送键,它必须是活动窗口(前景窗口)。
  2. 如果浏览器窗口状态发生了变化,最好将其恢复到原来的状态。
  3. 如果使用剪贴板,最好将其恢复到原始状态。

获取活动标签 URL 的步骤

  1. 查找名为 “iexplor” 且具有主窗口标题的进程。
  2. 记住原来的活动窗口和原来的浏览器窗口状态。
  3. 使浏览器窗口成为活动窗口。
  4. 记住剪贴板上的原始数据。
  5. 发送 ALT+DCTRL+C
  6. 复制剪贴板。
  7. 恢复原始剪贴板数据。
  8. 如果浏览器窗口已最小化,请将其最小化。
  9. 恢复原来的活动窗口。

缺点

  1. 更改浏览器窗口状态非常明显。
  2. 如果浏览器窗口未最小化,将其设为活动窗口会将其置于最前面。即使恢复了原来的活动窗口,它仍然是它后面的窗口。

代码要求

  1. 此代码使用Vanara NuGet Win32 API 包
  2. ClipboardSendKeys 需要引用 System.Windows.Forms
  3. Main 需要具有[STAThread] 属性才能使用Clipboard

代码

using System.Windows.Forms;
using System.Diagnostics;
using Vanara.PInvoke;

…

// Get all Internet Explorer processes with a window title 
Process[] ieProcs = Process.GetProcessesByName("iexplore")
    .Where(p => !string.IsNullOrEmpty(p.MainWindowTitle))
    .ToArray();

// Initialize a URL array to hold the active tab URL
string[] ieUrls = new string[ieProcs.Length];

for (int i = 0; i < ieProcs.Length; i++)
{
    Process proc = ieProcs[i];

    // Remember the initial window style of the process window
    User32_Gdi.WindowStyles initialWndStyles = (User32_Gdi.WindowStyles)User32_Gdi.GetWindowLongPtr(proc.MainWindowHandle, User32_Gdi.WindowLongFlags.GWL_STYLE);

    // Remember the initial foreground window
    IntPtr initialFgdWnd = (IntPtr)User32_Gdi.GetForegroundWindow();

    // Show the process window.
    // If it is minimized, it needs to be restored, if not, just show
    if (initialWndStyles.HasFlag(User32_Gdi.WindowStyles.WS_MINIMIZE))
    {
        User32_Gdi.ShowWindow(proc.MainWindowHandle, ShowWindowCommand.SW_RESTORE);
    }
    else
    {
        User32_Gdi.ShowWindow(proc.MainWindowHandle, ShowWindowCommand.SW_SHOW);
    }

    // Set the process window as the foreground window
    User32_Gdi.SetForegroundWindow(proc.MainWindowHandle);

    ieUrls[i] = null;

    // Remember the initial data on the clipboard and clear the clipboard
    IDataObject dataObject = Clipboard.GetDataObject();
    Clipboard.Clear();

    // Start a Stopwatch to timeout if no URL found
    Stopwatch sw = Stopwatch.StartNew();

    // Try to copy the active tab URL
    while (string.IsNullOrEmpty(ieUrls[i]) && sw.ElapsedMilliseconds < 1000)
    {
        // Send ALT+D
        SendKeys.SendWait("%(d)");
        SendKeys.Flush();

        // Send CRTL+C
        SendKeys.SendWait("^(c)");
        SendKeys.Flush();

        ieUrls[i] = Clipboard.GetText();
    }
    // Return the initial clipboard data to the clipboard
    Clipboard.SetDataObject(dataObject);

    // If the process window was initially minimized, minimize it
    if(initialWndStyles.HasFlag(User32_Gdi.WindowStyles.WS_MINIMIZE))
    {
        User32_Gdi.ShowWindow(proc.MainWindowHandle, ShowWindowCommand.SW_MINIMIZE);
    }

    // Return the initial foreground window to the foreground
    User32_Gdi.SetForegroundWindow(initialFgdWnd);
}

// Print the active tab URL's
for (int i = 0; i < ieUrls.Length; i++)
{
    Console.WriteLine(ieUrls[i]);
}

【讨论】:

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