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)