从chrome浏览器获取url

本文关键字:url 获取 浏览器 chrome | 更新日期: 2023-09-27 17:49:55

我正试图从chrome web浏览器获得url,版本33 -使用c#。我已经看了,并尝试了不同的建议,但到目前为止还没有运气。

我尝试过的事情:AutomationElement ->使用c#从Google Chrome获取当前标签's URL在33版Chrome浏览器中,使用自动化功能无法找到"地址和搜索栏"元素。我还尝试使用TreeWalker和这些我发现的唯一元素:

水平滚动条

少量返回

少量转发

垂直滚动条

少量返回

少量转发

"网页标题…"

系统菜单栏

系统

最小化

恢复

关闭

nde ->从c# windows窗体应用程序中获取当前URL

NDde.Client.DdeClient dde = new NDde.Client.DdeClient("Chrome", "WWW_GetWindowInfo");
dde.Connect();
NDde.Client.DdeClient dde = new NDde.Client.DdeClient("Chrome", "Chrome_OmniboxView");
dde.Connect();

既不工作…不能连接。

FindWindowEx ->

 [DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
public string GetChromeUrl(Process process)
{
    IntPtr handle = IntPtr.Zero;
    Process[] procsChrome = Process.GetProcessesByName("chrome");
    foreach (Process chrome in procsChrome)
    {
        // the chrome process must have a window
        if (chrome.MainWindowHandle == IntPtr.Zero)
        {
            continue;
        } else 
        {
            handle = chrome.MainWindowHandle;
            break;
        }
    }
    IntPtr urlHandle = FindWindowEx(handle, IntPtr.Zero, null, "Address and search bar");
    if (urlHandle != IntPtr.Zero)
    {
        Console.WriteLine("yes!");
    }
    return "";
}

也不工作

我走了多远

所以我使用UI Spy和inspect来查找Chrome 33中omnibox的名称。在UI间谍它不能被发现,但在检查我找到"地址和搜索栏",其中有url值我之后…问题是如何获取url信息?

任何想法?

从chrome浏览器获取url

不知道为什么自动化路由不工作在Chrome 33,从你的AutomationElement链接;我在Chrome 35实现了它,它确实工作得很好。一定要使用第一种实现,据说需要350ms才能找到URL。此外,我发现使用TreeScope。子树代替TreeScope。后裔似乎工作得快一点,虽然我没有执行定时测试或任何东西。

你试过从API获取它吗?

getURL−string chrome.runtime.getURL(string path)

链接到API