2

Firefox タブで開いているすべての URL を取得しようとしています。

これは私がこれまで行ってきたことです。コードはすべての Firefox の URL を取得しますが、開いている各ドキュメントからすべての URL を取得します。たとえば、フォーラムから埋め込まれた YouTube の URL などです。興味がある。

どうすればそれを修正できますか?.

また、例外をスローします

タイプ 'System.InvalidOperationException' の初回例外が Microsoft.VisualBasic.dll で発生しました

ここで文字列としてキャストしようとすると:

test.Add(TryCast(DirectCast(d.GetCurrentPattern(ValuePattern.Pattern), ValuePattern).Current.Value, String))

...しかし、コントロールを適切に「フィルタリング」して適切なURLを取得できれば、問題は解決するはずです。

また、このコードのもう 1 つの問題として、UI オートメーションにより、Firefox プロセスがタスクバーから最小化できなくなります。これらの UI オートメーション命令を実行した後、Firefox タスクバー ボタンを押してウィンドウを最大化/最小化することができません。

Imports System.Windows.Automation

Public Shared Function GetFirefoxUrls(process As Process) As List(Of String)

    If process Is Nothing Then
        Throw New ArgumentNullException("process")

    ElseIf process.MainWindowHandle = IntPtr.Zero Then
        Throw New ArgumentException("Invalid process.", "process")

    Else

        Dim element As AutomationElement =
            AutomationElement.FromHandle(process.MainWindowHandle)

        If element Is Nothing Then
            Return Nothing

        Else

            Dim docs As AutomationElementCollection =
                element.FindAll(TreeScope.Subtree,
                                New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document))

            If docs Is Nothing Then
                Return Nothing

            Else

                Dim test As New List(Of String)

                For Each d In docs
                    Try
                        test.Add(TryCast(DirectCast(d.GetCurrentPattern(ValuePattern.Pattern), ValuePattern).Current.Value, String))

                    Catch ex As Exception
                        Debug.WriteLine(ex.Message)
                    End Try
                Next

                Return test

            End If ' doc Is Nothing

        End If ' element Is Nothing

    End If ' process Is Nothing

End Function

使用法:

Dim urls As List(Of String) =
    GetFirefoxUrls(Process.GetProcessesByName("Firefox").First)
4

1 に答える 1

2

Inspect ツール (Windows 7 SDK に含まれています) を使用すると、すべての Firefox タブの URL EditControls に Name="Search or enter address" が含まれていることがわかります。要素の名前に基づいて PropertyCondition を作成できることを思い出せません (できるとは思いません)。しかし、Document 要素を繰り返し処理するときに、それぞれの Name プロパティを取得し、それらをリストに追加する前に「検索またはアドレスを入力」と比較できますか? -- つまり、このコード行の直前:

test.Add(TryCast(DirectCast(d.GetCurrentPattern(ValuePattern.Pattern), ValuePattern).Current.Value, String))

[編集] おっと、無視してください。話が早すぎました。Name="Search or enter address" EditControl は、ナビゲーション ツールバーの親内の単なるコントロールです...これには、各ページの URL ではなく、現在のタブの URL が含まれています。

プラン B: ドキュメント全体を反復処理し、ValueProperty 値を Firefox テスト セッションの既知の URL テキスト フラグメント フラグメント (「http://」などの文字列) と比較するテスト アプリを作成できますか? 次に、それらの要素が他の URL を含む要素と共通する属性 (共通の親名など) を確認します。

于 2014-08-04T22:55:15.103 に答える