【问题标题】:Get URL from Opera browser从 Opera 浏览器获取 URL
【发布时间】:2014-03-24 23:01:51
【问题描述】:

我正在尝试从 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" 可以请1 帮我解决这个问题吗?

注意:有几个问题在哪里标记了 chrome 和 opera,但在 SO 上没有 Opera 工作答案。

【问题讨论】:

    标签: c# opera


    【解决方案1】:

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

    【讨论】:

    • 非常感谢!我正在寻找的元素称为“Address field" if some1 将来需要它。
    【解决方案2】:

    您可以下载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();
    

    在函数中

    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;
        }
    }
    

    【讨论】:

    • 我已经尝试过了(在我的问题中有评论),但它对 Opera 不起作用,但我确实将它用于 firefox 和 IE。无论如何感谢您的回答!
    【解决方案3】:
    1. 获取Opera的进程ID:

      公共静态无效GetOperaURL() { Process[] OProcesses = Process.GetProcessesByName("opera"); 字符串 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; }

    【讨论】:

      【解决方案4】:

      更改了您的功能并使其尽可能简单。适用于当前的 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;
          }
      

      【讨论】:

        猜你喜欢
        • 2022-08-11
        • 2014-12-18
        • 1970-01-01
        • 1970-01-01
        • 2014-04-16
        • 2017-05-10
        • 2013-08-23
        • 2013-04-27
        相关资源
        最近更新 更多