您好,我正在使用此提取代码来获取Chrome上的当前网址,但只能获取有效的标签页面。我需要使用UI自动化从所有打开的选项卡。
我的工作代号:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 函数GetChromeUrl ( ByVal proc As Process ) As String If proc 。主窗口句柄 = IntPtr 。如果Dim element As System为零 ,则 返回 Nothing End 。视窗。自动化。)如果元素_ _ _ _ _ _什么都不是 Return Nothing End If Dim edit As System 。视窗。自动化。自动化元素 =元素。_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 当前。价值。ToString 结束 数 |
并在Form Load事件中使用以下代码调用它:
1 2 3 | 针对 每个proc As Process In Process。GetProcessesByName (“ chrome ” )MsgBox( proc.MainWindowTitle + “ ” + GetChromeUrl ( proc ))下一步_ |
你最好这样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 4 4_47 _ _ _ _ 51 | 进口无损检测。客户端'为 firefox 导入系统导入 NDde 库。运行时。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) > _ 私有 共享 函数FindWindowEx ( parentHandle As IntPtr, childAfter As IntPtr, className As String , windowTitle As String ) As IntPtr End 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 ) 如果长度> 0 Then SendMessage ( urlHandle、WM_GETTEXT、nChars、浅黄色) 浏览器网址=浅黄色。ToString ( ) 返回browserUrl 否则 返回browserUrl End If End Function Public shared Function GetChromeHandle ( ) As Intptr Dim ChromeHandle As IntPtr = Nothing Dim Allpro ( ) As Process = Process 。获取进程(); 对于 每个ProcessName = " chrome " ChromeHandle =专业版。MainWindowHandle Exit For End if Next返回ChromeHandle End Function ' USAGE FOR CHROME Dim CHandle As IntPtr = GetChromeHandle ( ) If Not CHandle , Equals ( Intptr . Zero ) Dim url As String= getChrome 网址 ( CHandle )如果 结束 |
来源并阅读更多
编辑:
我找到了自己的方式,对我有用
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 |
用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
希望能帮助到你 :))))
联系客服