我正在尝试检索在 Firefox 选项卡中打开的所有 url。
这就是我到目前为止所做的,代码获取所有 Firefox url,是的,但它也从每个打开的文档中检索所有 url,例如来自论坛的嵌入式 youtube url,等等......我不是感兴趣的。
我该如何解决?
而且它也会引发异常
Microsoft.VisualBasic.dll 中出现了“System.InvalidOperationException”类型的第一次机会异常
尝试在此处转换为 String 时:
test.Add(TryCast(DirectCast(d.GetCurrentPattern(ValuePattern.Pattern), ValuePattern).Current.Value, String))
...但我想如果我可以正确“过滤”控件以检索正确的 url,那么这个问题应该可以解决。
此代码的另一个问题是,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)