Microsoft Edge: Get Window URL and Title

后端 未结 7 1142
滥情空心
滥情空心 2020-12-06 15:33

Previously I was using ShellWindows() API for IE to get the window Title and URL for my application Now with new development, Microsoft Edge is new and has many features und

7条回答
  •  半阙折子戏
    2020-12-06 16:03

    I use VB.Net version, Windows 10 Home OS. Works for me. I get the page title and page URL. Code is part of one of my modules. Please copy and edit it as needed.

    '-----------------------------------------------------------------------------
    'Allow code to get Microsoft Edge URL & Title
    '   Add .Net references for UIAutomationClient & UIAutomationTypes
    Imports System.Windows.Automation
    '-----------------------------------------------------------------------------
    
    Public Function ActiveMicrosoftEdgeTitleAndURL(ByRef HadError As Boolean,
                                                   ByVal InhibitMsgBox As Boolean) As String()
    
        Dim i1 As Integer
        Dim tmp1 As String = "", tmp2() As String, METitle As String, MEURL As String
        Dim strME As String = "Microsoft Edge"
    
        'ActiveMicrosoftEdgeTitleAndURL(Index) = Page Title or "No Title" + Chr(255) + Page URL
    
        'If no Page URL then any Page Title is ignored.
        '   If the form is minimized to the taskbar the url is typically not available.
    
        HadError = False : ReDim tmp2(-1) : i1 = -1
    
        Try
            Dim conditions As Condition = Condition.TrueCondition
            Dim BaseElement As AutomationElement = AutomationElement.RootElement
            Dim elementCollection As AutomationElementCollection = BaseElement.FindAll(TreeScope.Children, conditions)
            Dim AE As AutomationElement
            For Each AE In elementCollection
                If AE IsNot Nothing Then
                    tmp1 = AE.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString
                    If StrComp(Strings.Right(tmp1, strME.Length), strME, vbTextCompare) = 0 Then
                        MEURL = "" : METitle = ""
                        '-----------------------------------------------------------------------------------------------------------
                        Dim AE1 As AutomationElement = _
                            AE.FindFirst(TreeScope.Subtree, New PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar"))
                        METitle = AutomationElementText(AE1)
                        METitle = Trim(METitle)
                        '-----------------------------------------------------------------------------------------------------------
                        AE1 = AE.FindFirst(TreeScope.Subtree, New PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"))
                        MEURL = AutomationElementText(AE1)
                        MEURL = Trim(MEURL)
                        '-----------------------------------------------------------------------------------------------------------
                        If MEURL <> "" Then
                            If METitle = "" Then METitle = "No Title"
                            i1 = i1 + 1 : Array.Resize(tmp2, i1 + 1)
                            tmp2(i1) = METitle + Chr(255) + MEURL
                        End If
                    End If
                End If
            Next
        Catch ex As Exception
            HadError = True
            MsgBox("Function AutomationElementData system error." + vbCr + vbCr + ex.ToString, vbExclamation)
        End Try
    
        Return tmp2
    
    End Function
    
    Private Function AutomationElementText(ByRef AE As AutomationElement) As String
    
        Dim MyPattern As AutomationPattern = ValuePattern.Pattern
        Dim MyPattern1 As AutomationPattern = TextPattern.Pattern
        Dim objPattern As Object = Nothing
        Dim txt As String = ""
    
        'Any error just return a null string. !r
    
        If AE.TryGetCurrentPattern(MyPattern, objPattern) Then
            Dim AEValuePattern As ValuePattern = AE.GetCurrentPattern(MyPattern)
            txt = AEValuePattern.Current.Value
        Else
            If AE.TryGetCurrentPattern(MyPattern1, objPattern) Then
                Dim AETextPattern As TextPattern = AE.GetCurrentPattern(MyPattern1)
                txt = AETextPattern.DocumentRange.GetText(-1)
            End If
        End If
    
        Return txt
    
    End Function
    

提交回复
热议问题