2013-04-30 83 views
2

您好,我有這段代碼可以在Chrome上獲取當前網址,但只會獲得活動標籤網址。我需要使用UI Automation從所有打開的選項卡中獲取URL。使用VB .Net和UI自動獲取谷歌瀏覽器中所有打開的標籤的網址

我的工作代碼:

Function GetChromeUrl(ByVal proc As Process) As String 
    If proc.MainWindowHandle = IntPtr.Zero Then 
    Return Nothing 
End If 

Dim element As System.Windows.Automation.AutomationElement = AutomationElement.FromHandle(proc.MainWindowHandle) 
If element Is Nothing Then 
    Return Nothing 
End If 

Dim edit As System.Windows.Automation.AutomationElement = element.FindFirst(TreeScope.Children, New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)) 
Return (edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value.ToString 
End Function 

,並在窗體的Load事件中使用此代碼調用它:

For Each proc As Process In Process.GetProcessesByName("chrome") 
    MsgBox(proc.MainWindowTitle + " " + GetChromeUrl(proc)) 
Next 
+0

不爲我工作... – 2016-08-26 07:28:50

回答

2

你最好嘗試這種方式

Imports NDde.Client 'import the NDde library for firefox 
Imports System.Runtime.InteropServices 

'For Chrome 
Private Const WM_GETTEXTLENGTH As Integer = &He 
Private Const WM_GETTEXT As Integer = &Hd 

<DllImport("user32.dll")> _ 
Private Shared Function SendMessage(hWnd As IntPtr, Msg As UInteger, wParam As Integer, lParam As Integer) As Integer 
End Function 
<DllImport("user32.dll")> _ 
Private Shared Function SendMessage(hWnd As IntPtr, Msg As UInteger, wParam As Integer, lParam As StringBuilder) As Integer 
End Function 
<DllImport("user32.dll", SetLastError := True)> _ 
Private Shared Function FindWindowEx(parentHandle As IntPtr, childAfter As IntPtr, className As String, windowTitle As String) As IntPtr 
End Function 

Public Shared Function getChromeUrl(winHandle As IntPtr) As String 
    Dim browserUrl As String = Nothing 
    Dim urlHandle As IntPtr = FindWindowEx(winHandle, IntPtr.Zero, "Chrome_AutocompleteEditView", Nothing) 
    Const nChars As Integer = 256 
    Dim Buff As New StringBuilder(nChars) 
    Dim length As Integer = SendMessage(urlHandle, WM_GETTEXTLENGTH, 0, 0) 
    If length > 0 Then 
     SendMessage(urlHandle, WM_GETTEXT, nChars, Buff) 
     browserUrl = Buff.ToString() 

     Return browserUrl 
    Else 
     Return browserUrl 
    End If 

End Function 

Public shared Function GetChromeHandle() As Intptr 
Dim ChromeHandle As IntPtr = Nothing 
Dim Allpro() As Process = Process.GetProcesses(); 
For Each pro As Process in Allpro 
    if pro.ProcessName = "chrome" 
    ChromeHandle = pro.MainWindowHandle 
    Exit For 
    End if 
Next  
Return ChromeHandle 
End Function 

'USAGE FOR CHROME 
Dim CHandle As IntPtr = GetChromeHandle() 
If Not CHandle,Equals(Intptr.Zero) 
Dim url As String = getChromeUrl(CHandle) 
End If 

Source and read more

編輯:

,我發現我自己的方式和它的工作對我來說

Dim appAs String = "chrome" 
Dim proc As System.Diagnostics.Process = GetBrowser(app) 
... 
Private Function GetBrowser(ByVal appName) As System.Diagnostics.Process 
    Dim pList() As System.Diagnostics.Process = 
System.Diagnostics.Process.GetProcessesByName(app) 
    For Each proc As System.Diagnostics.Process In pList 
     If proc.ProcessName = appThen 
      Return proc 
     End If 
    Next 
    Return Nothing 
End Function 

用法:

If proc IsNot Nothing Then 
    Dim browserName as string = "Google Chrome" 
    Dim className as String = "Edit" 
    Dim s As String = 
GetCurrentUrl(proc.MainWindowHandle, browserName, className, ComboBox1) 
    If s <> "" Then 
     Msgbox.show(s) 
     ComboBox1.SelectedIndex = 0 'Window list 
    Else 

    End If 
Else 
    Label1.Text = browserName & " is not available" 
end If 

希望它能幫助:))))

+1

嗨!如果我可能會問先生,GetCurrentUrl函數有什麼作用? – 2014-03-05 01:39:55

+0

對我來說根本不起作用,,,是不是有一種更簡單的方法來做到這一點? – 2016-08-25 09:55:07

相關問題