Search code examples
c#opera

Get URL from Opera browser


I am trying to get URL from Opera browser, I assumed it is the same as for Chrome but I was wrong since it doesn't work this way.

whatever I have done so far is:

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

I have tried it with both NDDE client and automation element, but both are failed :( I think in automation element the problem is in this line

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

Perhaps for opera it isn't "Address and search bar" can some 1 help me to solve the issue please?

NOTE: There are couple question where chrome and opera tagged but there is no Opera working answer inside on SO.


Solution

  • You can use Inspect which offers the same functionality as UISpy to get the AutomationId from the Opera UI. I assume the reason it is not working is because it is named differently in Opera.