首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# UIA获取微软边缘URL

C# UIA获取微软边缘URL
EN

Stack Overflow用户
提问于 2017-09-28 21:43:58
回答 1查看 1.8K关注 0票数 1

我目前正在使用UIA从Chrome,Firefox和IE11获取网址,方法如下:

代码语言:javascript
复制
string processName = "";

if (browser.Equals(BrowserType.GOOGLE_CHROME))
    processName = "chrome";
else if (browser.Equals(BrowserType.FIREFOX))
    processName = "firefox";
else if (browser.Equals(BrowserType.INTERNET_EXPLORER))
    processName = "iexplore";
else if (browser.Equals(BrowserType.MICROSOFT_EDGE))
    processName = "MicrosoftEdgeCP";

foreach(Process process in Process.GetProcessesByName(processName))
{
    string url = GetURLFromProcess(process, browser);
    if (url == null)
        continue;

    return url;
}

...

private string GetURLFromProcess(Process process, BrowserType browser)
{
    if (process == null)
        throw new ArgumentNullException("process");

    if (process.MainWindowHandle == IntPtr.Zero)
        return null;

    AutomationElement elm = AutomationElement.FromHandle(process.MainWindowHandle);
    if (elm == null)
        return null;
    string nameProperty = "";

    if (browser.Equals(BrowserType.GOOGLE_CHROME))
        nameProperty = "Address and search bar";
    else if (browser.Equals(BrowserType.FIREFOX))
        nameProperty = "Search or enter address";
    else if (browser.Equals(BrowserType.INTERNET_EXPLORER))
        nameProperty = "Address and search using Bing";
    else if (browser.Equals(BrowserType.MICROSOFT_EDGE))
        nameProperty = "Search or enter web address";

    AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Subtree, new AndCondition(
        new PropertyCondition(AutomationElement.NameProperty, nameProperty, PropertyConditionFlags.IgnoreCase),
        new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));

    if (elmUrlBar != null)
    {
        return ((ValuePattern)elmUrlBar.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
    }

    return null;
}

使用Inspect,我可以看到Edge中的URL栏由“搜索或输入网址”标识。然而,尽管上面的方法适用于其他web浏览器,但Edge似乎并非如此。

'elmUrlBar‘总是以null结尾并且找不到。

EN

回答 1

Stack Overflow用户

发布于 2018-04-06 05:06:26

此代码适用于最新版本的Firefox、Internet Explorer、MS Edge和Chrome。此方法比按名称查找地址栏更快。这种方法不依赖于系统的局部性。

代码语言:javascript
复制
private string GetURLFromProcess(Process process, BrowserType browser)
{
    if (process == null)
        throw new ArgumentNullException("process");

    if (process.MainWindowHandle == IntPtr.Zero)
        return null;

    AutomationElement elm = AutomationElement.FromHandle(process.MainWindowHandle);
    if (elm == null)
        return null;
    string nameProperty = "";

    if (browser.Equals(BrowserType.GOOGLE_CHROME))
        {
            var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
            if (elm1 == null) { return null; } // not the right chrome.exe
            var elm2 = elm1.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
            var elm3 = elm2.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
            var elm4 = elm3.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
            var elm5 = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
            var elmUrlBar = elm5.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
            var url = ((ValuePattern)elmUrlBar.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
            return url;
        }
    else if (browser.Equals(BrowserType.FIREFOX))
        {
             AutomationElement elm2 = elm.FindFirst(TreeScope.Children, new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar),
                    new PropertyCondition(AutomationElement.IsInvokePatternAvailableProperty, false)));
            if (elm2 == null)
                return null;
            var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ComboBox));
            if (elm3 == null)
                return null;
            var elmUrlBar = elm3.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
            if (elmUrlBar != null)
            {
                var url = ((ValuePattern)elmUrlBar.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
                url;
            }
        }
    else if (browser.Equals(BrowserType.INTERNET_EXPLORER))
        {
             AutomationElement elm2 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "ReBarWindow32"));
             if (elm2 == null)
                 return null;
             AutomationElement elmUrlBar = elm2.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
             var url = ((ValuePattern)elmUrlBar.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
             return url;
        }
    else if (browser.Equals(BrowserType.MICROSOFT_EDGE))
        {
            var elm2 = elm.FindFirst(TreeScope.Children, new AndCondition(
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
            new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));

            var elmUrlBar = elm2.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));

            var url = ((TextPattern)elmUrlBar.GetCurrentPattern(TextPattern.Pattern)).DocumentRange.GetText(int.MaxValue);
            return url;
        }

    return null;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46470737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档