4

I am trying to get the url from a chrome web browser, version 33 - using C#. I have looked at, and tried, different suggestions here on SO - but so far no luck.

Things I have tried: AutomationElement -> Getting the current tab's URL from Google Chrome using C# The "Address and search bar" element can't be found by use of Automation in the 33 version of Chrome web browser. I also tried using TreeWalker and these where the only elements I found:

Horizontal Scroll Bar

Back by small amount

Forward by small amount

Vertical Scroll Bar

Back by small amount

Forward by small amount

"Title of web page..."

System Menu Bar

System

Minimize

Restore

Close

NDde -> Retrieve current URL from C# windows forms application

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();

Neither works... can't 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 "";
}

Doesn't work either...

How far I got

So I have used UI Spy and inspect to look for the name of the omnibox in Chrome 33. In UI spy it can't be found at all, but in inspect I find the "Address and search bar", which has the url value I am after... the question is how do I get hold of that url info?

Any idea?

Community
  • 1
  • 1
user3409403
  • 41
  • 1
  • 2

2 Answers2

1

Not sure why the Automation route didn't work in Chrome 33, from your AutomationElement link; I implemented it in Chrome 35 and it does work fine. Be sure to use the first implementation which supposedly takes 350ms to find the URL. Also, I found that using TreeScope.Subtree instead of TreeScope.Descendants seems to work a little faster, though I haven't performed timed tests or anything.

MQuiggGeorgia
  • 729
  • 1
  • 7
  • 10
-1

Have you tried getting it from the API ?

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

Here is the link to source : Link to API

LeRoy
  • 4,189
  • 2
  • 35
  • 46