从Opera浏览器获取URL [英] Get URL from Opera browser

查看:130
本文介绍了从Opera浏览器获取URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经使用NDDE客户端和自动化元素进行了尝试,但是都失败了:( 我认为自动化元素的问题就在这行

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

也许不是歌剧"Address and search bar",请问有1个可以帮助我解决问题吗?

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

注意:存在几个问题,其中标记了Chrome和Opera,但在SO上却没有Opera的有效答案.

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

推荐答案

您可以使用

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.

这篇关于从Opera浏览器获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆