2016-05-25 86 views
0

私はこの質問のタイトルと同じキーワードでgoogleで検索しています。 一部のコードは、スタックオーバーフローリンクであっても、機能しないか複雑な説明ではありません。 リンク作業を1件見つかりましたhere。 しかし、そのコードの結果はページタイトルであり、URLではありません。 このコードを作成して現在のURLを取得するにはどうすればよいですか?ここ コード、別のフォーラムから、解決アクティブなタブの現在のURLを取得するchrome vb net?

'//Grab all the Chrome processes 
Dim chrome() As Process = Process.GetProcessesByName("chrome") 

'//Exit if chrome isn't running 
If chrome.Length <= 0 Then Exit Sub 

For Each chromeProcess As Process In chrome 

'//If the chrome process doesn't have a window handle then ignore it 
If chromeProcess.MainWindowHandle <> IntPtr.Zero Then 

    '//To find the tabs we first need to locate something reliable - the 'New Tab' button 
    Dim rootElement As AutomationElement = AutomationElement.FromHandle(chromeProcess.MainWindowHandle) 
    Dim condNewTab As Condition = New PropertyCondition(AutomationElement.NameProperty, "New Tab") 
    Dim elemNewTab As AutomationElement = rootElement.FindFirst(TreeScope.Descendants, condNewTab) 

    '//Get the tabstrip by getting the parent of the 'new tab' button 
    Dim tWalker As TreeWalker = TreeWalker.ControlViewWalker 
    Dim elemTabStrip As AutomationElement = tWalker.GetParent(elemNewTab) 

    '//Loop through all the tabs and get the names which is the page title 
    Dim tabItemCondition As Condition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem) 
    For Each tabItem As AutomationElement In elemTabStrip.FindAll(TreeScope.Children, tabItemCondition) 
     Debug.WriteLine(tabItem.Current.Name) 
    Next 

End If 
Next 
+0

Awesomium(http://www.awesomium.com/#download)を使用してみましたか?これに関する詳細はこちら:http://stackoverflow.com/questions/1534843/google-chrome-control-for-net?lq=1 – Dustin

+0

まだ、しかし、私はいくつかのトピックを読んで、それはWebブラウザを作るのが好きですVBネット、私が必要なのはクロムの現在のURLをキャプチャすることです。私に特定のリンクを教えてもらえますか? –

答えて

1

Imports System.Windows.Automation 

Module GoogleChrome 
Private Const ChromeProcess As [String] = "chrome" 
Private Const AddressCtl As [String] = "Address and search bar" 

Public Function GetChromeActiveWindowUrl() As [String] 
    Dim procs = Process.GetProcessesByName(ChromeProcess) 

    If (procs.Length = 0) Then 
     Return [String].Empty 
    End If 

    Return procs _ 
    .Where(Function(p) p.MainWindowHandle <> IntPtr.Zero) _ 
    .Select(Function(s) GetUrlControl(s)) _ 
    .Where(Function(p) p IsNot Nothing) _ 
    .Select(Function(s) GetValuePattern(s)) _ 
    .Where(Function(p) p.Item2.Length > 0) _ 
    .Select(Function(s) GetValuePatternUrl(s)) _ 
    .FirstOrDefault 

End Function 

Private Function GetUrlControl(_ 
    proses As Process) _ 
    As AutomationElement 

    Dim propCondition = _ 
     New PropertyCondition(_ 
     AutomationElement.NameProperty, _ 
     AddressCtl) 
    Return AutomationElement _ 
     .FromHandle(proses.MainWindowHandle) _ 
     .FindFirst(_ 
      TreeScope.Descendants, _ 
      propCondition) 

End Function 

Private Function GetValuePatternUrl(_ 
    element As Tuple(Of _ 
    AutomationElement, AutomationPattern())) As [String] 

    Dim ap = element.Item2(0) 
    Dim ovp = element.Item1.GetCurrentPattern(ap) 
    Dim vp = CType(ovp, ValuePattern) 

    Return vp.Current.Value 
End Function 

Private Function GetValuePattern(_ 
    element As AutomationElement) _ 
As Tuple(Of _ 
      AutomationElement, _ 
      AutomationPattern()) 

    Return New Tuple(Of _ 
      AutomationElement, _ 
      AutomationPattern())(_ 
      element, _ 
      element.GetSupportedPatterns()) 
End Function 



End Module 
1

あなたはそれがGoogle Chromeの55とFirefoxのために働くの.NET Framework 2.0を使用している場合。メソッド名( "CHROME"または "FIREFOX")にプロセス名を送信します。また、UIAutomationClient.dllをインポートする必要があります。私はそれが誰かを助けることを望む。

private string GetBrowserUrlActivaChromeFirefox(string process) 
    { 
     Process[] procsChrome = Process.GetProcessesByName(process); 
     foreach (Process chrome in procsChrome) 
     { 
      if (chrome.MainWindowHandle == IntPtr.Zero) 
       continue; 


      UIAutomationClient.CUIAutomation element = new UIAutomationClient.CUIAutomation(); 
      UIAutomationClient.IUIAutomationElement root = element.ElementFromHandle(chrome.MainWindowHandle); 

      if (element == null) 
       return ""; 

      UIAutomationClient.IUIAutomationCondition condition1 = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_ProcessIdPropertyId, chrome.Id); 
      UIAutomationClient.IUIAutomationCondition condition2 = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_IsControlElementPropertyId, true); 
      UIAutomationClient.IUIAutomationCondition condition3 = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_IsContentElementPropertyId, true); 
      UIAutomationClient.IUIAutomationCondition condition4 = element.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_ControlTypePropertyId, UIAutomationClient.UIA_ControlTypeIds.UIA_EditControlTypeId); 

      UIAutomationClient.IUIAutomationCondition[] conditions = new UIAutomationClient.IUIAutomationCondition[4]; 

      conditions[0] = condition1; 
      conditions[1] = condition2; 
      conditions[2] = condition3; 
      conditions[3] = condition4; 

      UIAutomationClient.IUIAutomationCondition andCondition = element.CreateAndConditionFromArray(conditions); 

      UIAutomationClient.IUIAutomationElement elementx = root.FindFirst(UIAutomationClient.TreeScope.TreeScope_Descendants, andCondition); 

      return ((UIAutomationClient.IUIAutomationValuePattern)elementx.GetCurrentPattern(UIAutomationClient.UIA_PatternIds.UIA_ValuePatternId)).CurrentValue.ToString(); 

     } 

     return string.Empty; 
    } 
関連する問題