从Opera浏览器获取URL

我试图从Opera浏览器获取URL,我认为它与Chrome相同,但我错了,因为它不能以这种方式工作。

无论我到目前为止做了什么:

public static string GetURL(IntPtr intPtr, string programName, out string url) { string temp = null; if (programName.Equals("opera")) { // // there are always multiple opera processes, so we have to loop through all of them to find the // // process with a Window Handle and an automation element of name "Address and search bar" /* string x = ""; DdeClient dde = new DdeClient("opera", "WWW_GetWindowInfo"); try { //temp := RequestData('0xFFFFFFFF'); dde.Connect(); string url1 = dde.Request("URL", int.MaxValue); string[] text = url1.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries); dde.Disconnect(); } catch (Exception) { x = "failed"; } */ Process[] procsOpera = Process.GetProcessesByName("opera"); foreach (Process opera in procsOpera) { // the chrome process must have a window if (opera.MainWindowHandle == IntPtr.Zero) { continue; } // find the automation element AutomationElement elm = AutomationElement.FromHandle(opera.MainWindowHandle); AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar")); // if it can be found, get the value from the URL bar if (elmUrlBar != null) { AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns(); if (patterns.Length > 0) { ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]); temp = val.Current.Value.ToString(); url = val.Current.Value.ToString(); } else { temp = ""; url = ""; } } else { temp = ""; url = ""; } } } url = temp; return temp; } 

我用NDDE客户端和自动化元素都试过了,但两者都失败了:(我认为在自动化元素中问题出在这一行

 AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar")); 

也许对于歌剧来说,不是"Address and search bar"可以帮我解决一下这个问题吗?

注意:有几个问题,铬和歌剧标记,但在SO内没有Opera工作答案。

您可以使用Inspect提供与UISpy相同的function,以从Opera UI获取AutomationId。 我认为它不起作用的原因是因为它在Opera中的命名不同。

您可以下载NDde dll 。 添加引用后,只需将此代码复制到您想要获取URL的位置即可。

NDde DLL下载: http ://ndde.codeplex.com/

 DdeClient dde = new DdeClient("opera", "WWW_GetWindowInfo"); dde.Connect(); string url = dde.Request("URL", int.MaxValue); string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries); dde.Disconnect(); 

如果你想获得firefox数据,只需将opera更改为firefox

 DdeClient dde = new DdeClient("firefox", "WWW_GetWindowInfo"); dde.Connect(); string url = dde.Request("URL", int.MaxValue); string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries); dde.Disconnect(); 

在一个function

 private string GetBrowserURL(string browser) { try { DdeClient dde = new DdeClient(browser, "WWW_GetWindowInfo"); dde.Connect(); string url = dde.Request("URL", int.MaxValue); string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries); dde.Disconnect(); return text[0].Substring(1); } catch { return null; } } 
  1. 获取opera的进程ID:

     public static void GetOperaURL()
     {
        Process [] OProcesses = Process.GetProcessesByName(“opera”);
        string url = null;
        foreach(OProcesses中的处理过程)
        {
            if(proc.MainWindowHandle!= IntPtr.Zero)
            {
                 url = getOperaUrlFromProcess(proc);
                打破;
             }
         }
     } 

  2. 使用自动化元素来确定URL

    private static string getOperaUrlFromProcess(Process proc) { // find the automation element AutomationElement elm = AutomationElement.FromHandle(proc.MainWindowHandle); // manually walk through the tree, searching using TreeScope.Descendants is too slow (even if it's more reliable) AutomationElement elmUrlBar = null; try { var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Browser container")); if (elm1 == null) { return null; } elmUrlBar = elm.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address field")); } catch { // Chrome has probably changed something, and above walking needs to be modified. :( // put an assertion here or something to make sure you don't miss it return null; } // make sure it's valid if (elmUrlBar == null) { // it's not.. return null; } // there might not be a valid pattern to use, so we have to make sure we have one AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns(); if (patterns.Length == 1) { string ret = ""; try { ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value; } catch { } if (ret != "") { return ret; } return null; } return null; }

改变了你的function,使其尽可能简单。 适用于当前Opera版本47.0

 public static string GetOperaURL() { string url = ""; Process[] procsOpera = Process.GetProcessesByName("opera"); foreach (Process opera in procsOpera) { // the chrome process must have a window if (opera.MainWindowHandle == IntPtr.Zero) { continue; } // find the automation element AutomationElement elm = AutomationElement.FromHandle(opera.MainWindowHandle); AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address field")); // if it can be found, get the value from the URL bar if (elmUrlBar == null) continue; AutomationPattern pattern = elmUrlBar.GetSupportedPatterns().FirstOrDefault(wr=>wr.ProgrammaticName == "ValuePatternIdentifiers.Pattern"); if (pattern == null) continue; ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(pattern); url = val.Current.Value; break; } return url; }