Share via

Microsoft Edge URL

harsha111111 121 Reputation points
2021-07-14T12:08:06.887+00:00

I've been searching for code that gives me urls that are searched currently in Microsoft edge

I tried below code, it works till current window is Edge and then it gives null at tabelement

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Interop.UIAutomationClient;

namespace EdgeUrl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Start();
    }

    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    private void timer1_Tick(object sender, EventArgs e)
    {
        const int UIA_NamePropertyId = 30005;
        const int UIA_ClassNamePropertyId = 30012;
        const int UIA_NativeWindowHandlePropertyId = 30020;


        IUIAutomation uiA = new CUIAutomation();
        IUIAutomationElement rootElement = uiA.GetRootElement();

        IUIAutomationCacheRequest cacheRequest = uiA.CreateCacheRequest();
        cacheRequest.AddProperty(UIA_NamePropertyId);

        IUIAutomationCondition windowCondition = uiA.CreatePropertyCondition(UIA_NativeWindowHandlePropertyId, GetForegroundWindow());
        IUIAutomationElement windowElement = rootElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, windowCondition, cacheRequest);
        if (windowElement == null)
        {

        }
        else
        {

            IUIAutomationCondition edgeCondition = uiA.CreatePropertyCondition(UIA_NamePropertyId, "Microsoft Edge");
            IUIAutomationElement edgeElement = windowElement.FindFirstBuildCache(TreeScope.TreeScope_Subtree, edgeCondition, cacheRequest);
            if (edgeElement == null)
            {

            }
            else
            {

                IUIAutomationCondition tabCondition = uiA.CreatePropertyCondition(UIA_ClassNamePropertyId, "TabWindowClass");
                IUIAutomationElement tabElement = edgeElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, tabCondition, cacheRequest);
                if (tabElement == null)
                {

                }

                else
                {

                    IUIAutomationCondition ieCondition = uiA.CreatePropertyCondition(UIA_ClassNamePropertyId, "Internet Explorer_Server");
                    IUIAutomationElement ieElement = edgeElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, ieCondition, cacheRequest);
                    if (ieElement == null)
                    {


                    }
                    else
                    {

                        string url = ieElement.CachedName;
                        string title = tabElement.CachedName;
                        MessageBox.Show(url, title);
                    }

                }

            }

        }
    } 
}
}

If anyone knows why it is happening please do help and if any better code is available to get current url please post
Thanks

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Castorix31 91,876 Reputation points
2021-07-14T12:28:27.083+00:00

This test works for me (code tested in a click on a Button...) =>

IntPtr hWndEdge = IntPtr.Zero;
System.Diagnostics.Process[] procEdge = System.Diagnostics.Process.GetProcessesByName("msedge");
if (procEdge.Length <= 0)
    Console.WriteLine("Microsoft Edge is not running");
else
{
    foreach (System.Diagnostics.Process proc in procEdge)
    {
        if (proc.MainWindowHandle == IntPtr.Zero)
            continue;
        else
        {
            hWndEdge = proc.MainWindowHandle;
            break;
        }
    }
}
if (hWndEdge != IntPtr.Zero)
{ 
    IUIAutomation pUIAutomation = new CUIAutomation();
    IUIAutomationElement windowElement = pUIAutomation.ElementFromHandle(hWndEdge);
    if (windowElement != null)
    {
        IUIAutomationElementArray elementArray = null;
        IUIAutomationCondition condition = null;
        condition = pUIAutomation.CreateTrueCondition();
        elementArray = windowElement.FindAll(TreeScope.TreeScope_Descendants, condition);
        IUIAutomationElement elementAddressBar = null;
        if (elementArray != null)
        {
            int nNbItems = elementArray.Length;
            for (int nItem = 0; nItem <= nNbItems - 1; nItem++)
            {
                IUIAutomationElement element = elementArray.GetElement(nItem);
                string sName = element.CurrentName;
                string sAutomationId = element.CurrentAutomationId;
                Console.WriteLine("Name : {0} - AutomationId : {1}", sName, sAutomationId);
                IUIAutomationElement5 element5 = (IUIAutomationElement5)element;
                int nLandmakType = element5.CurrentLandmarkType;
                if (nLandmakType == 80004) // UIA_LandmarkTypeIds.UIA_SearchLandmarkTypeId
                {
                    elementAddressBar = element;
                    break;
                }
            }
            if (elementAddressBar != null)
            {
                Object pPattern = null;
                pPattern = elementAddressBar.GetCurrentPattern(10018); // UIA_PatternIds.UIA_LegacyIAccessiblePatternId
                if (pPattern != null)
                {
                    IUIAutomationLegacyIAccessiblePattern pLegacyIAccessiblePattern = (IUIAutomationLegacyIAccessiblePattern)pPattern;
                    String sValue = pLegacyIAccessiblePattern.CurrentValue;
                    MessageBox.Show(String.Format("Current URL = {0}", sValue), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.