Avatar of mikesung99
mikesung99
 asked on

Foregound Web Browser Control Programmitcally in Visual Basic/C# 2003/2005

Hi,

We're investigating the possibilty of controlling a web browser (which is running in the foreground) programmitcally from either Visual Basic/C# 2003/2005 what we want to be able to prove is the functionality where the VB functionality can select and identify an active browser session on a user's desktop, navigate to a specific URL which contains a form like interface (such as google), populate a specific field in the form with information and then submit the data. Once the submit has been "clicked/invoked" the actie browser will display the results in the foreground.

We would just like to investigate the feasiblity of this and wether or not this sounds feasible? If so, we would appreciate if anyone could provide us with some examples, pointers or Classes that we can look at in order to progress the investigation.
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
mikesung99

8/22/2022 - Mon
hes

Yes it very feasible, you have two ways of doing it.
1) you can initiate the browser session and control that instance of the browser (easiest)
2) Find the hwnd of the current active browser (harder due to the user can have more than one browser instance open at one time).
Either way it is possible to then navigate fill form items and submit the form.
SOLUTION
hes

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mikesung99

ASKER
Thanks for the info - I've had a look at both of the suggestions and I think for our purposes and what we're trying to prove, we are looking into obtaining the hwnd of a current browser session. We been looking the various windows API functions available and have found the attached code sample which will allow us to identify the specific active browser session.

We're making the assumption that we will identify the browser by the window Name (e.g. "Google - Windows Internet Explorer") and that there will not be 2 instances of the browser with the same page open.

In the attached code snippet, the code is able to identify the specific browser window and we've looked at the SendMessage API function which will allow us to control the window and do things like close the broswer window. To move forward, we would like to now, having obtained the particular window, navigate to the specific URL and then fill in form elements.

Do you have any suggestions on if I am able to acomplish this using the SendMessage function or do I need to look at other API functions?

Thanks




    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        fEnumWindows()
    End Sub
 
    Public Const MAX_PATH As Short = 260
    Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Integer, ByVal lParam As Integer) As Integer
    Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Public Const WM_CLOSE As Short = &H10S
 
    Public Function EnumFunc(ByVal hwnd As Integer, ByVal lpData As Integer) As Integer
        Dim lResult As Integer
        Dim sWndName As String
        Dim szClassName As String
 
        EnumFunc = 1
        szClassName = Space(MAX_PATH)
        sWndName = Space(MAX_PATH)
 
        lResult = GetClassName(hwnd, szClassName, MAX_PATH)
        szClassName = Microsoft.VisualBasic.Left(szClassName, lResult)
 
 
        lResult = GetWindowText(hwnd, sWndName, MAX_PATH)
        sWndName = Microsoft.VisualBasic.Left(sWndName, lResult)
 
        'Dim vreturnvalue As Object
        If InStr(sWndName, "Google - Windows") > 0 Then
            'vreturnvalue = SendMessage(hwnd, WM_CLOSE, &O0S, &O0S)
            MsgBox("Broswewr Session Identified")
            'Now Need to navigate to specific URL..
        End If
 
    End Function
 
    Public Delegate Function EnumFuncDeleg(ByVal hwnd As Integer, ByVal lpData As Integer) As Integer
 
    Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumFuncDeleg, ByVal lParam As Integer) As Integer
 
 
    Public Function fEnumWindows() As Boolean
        Dim hwnd As Integer
        Call EnumWindows(AddressOf EnumFunc, hwnd)
    End Function

Open in new window

Bob Learned

How may I help?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
mikesung99

ASKER
Hi Learned One,

We are trying to investagte if it is possible to develop some code which will allow us to progratically control a specific browser instance. I.e. there is an active browser on the desktop and there is code which will interact with the active browser session, navigate to a specific URL (on which contains a form with a submit type button), fill in a specific form field and then submit the form.

We've been loooking into some Windows API functions (using Visual Basic .NET 2005) and have so far identified code which will allow us to determine the hwnd handle of a specific browser window.

We're making the assumption that we will identify the browser by the window Name (e.g. "Google - Windows Internet Explorer") and that there will not be 2 instances of the browser with the same page open.

We've come across the SendMessage API function which will allow us to control the window and do things like close the broswer window. To move forward, we would like to now, having obtained the particular window, navigate to the specific URL and then fill in form elements.

Do you have any suggestions on if I am able to acomplish this using the SendMessage function or do I need to look at other API functions?

Thanks

Michael
Bob Learned

Michael,

I have a lot of little snippets of code to work with Internet Explorer instances, and each may prove to be useful, but they aren't all in one place.
Bob Learned

Class to find open Internet Explorer windows:


' Add a COM reference to 'Microsoft Internet Controls' to the project.
' Add a .NET reference to Microsoft.mshtml to the project.
 
' NOTE:
' Don't add an Imports mshtml if you want your Intellisense to work.
 
Imports System
Imports System.Collections.Generic
Imports SHDocVw
 
Public Class InternetExplorerWindows
 
  Public Shared Function GetWindows() As Dictionary(Of String, String)
    Dim list As New Dictionary(Of String, String)
    For Each window As InternetExplorer In New ShellWindows()
 
      ' Skip Windows Explorer instances, since the Document type is IShellFolderViewDual2.
      If TypeOf window.Document Is mshtml.HTMLDocument Then
        list.Add(window.LocationURL, CType(window.Document, mshtml.HTMLDocument).body.innerHTML)
      End If
    Next window
    Return list
  End Function
 
  Public Shared Function GetIFrameHTML(ByVal frameID As String) As Dictionary(Of String, String)
    Dim list As New Dictionary(Of String, String)
    For Each window As InternetExplorer In New ShellWindows()
 
      ' Skip Windows Explorer instances, since the Document type is IShellFolderViewDual2.
      If TypeOf window.Document Is mshtml.HTMLDocument Then
        Dim doc As mshtml.HTMLDocument = CType(window.Document, mshtml.HTMLDocument)
        If doc.getElementById(frameID) IsNot Nothing Then
          Dim frame As mshtml.HTMLIFrame = CType(doc.getElementById(frameID), mshtml.HTMLIFrame)
          Dim window2 As mshtml.HTMLWindow2 = CType(frame.contentWindow, mshtml.HTMLWindow2)
          Dim windowDocument As mshtml.IHTMLDocument2 = CType(window2.document, mshtml.IHTMLDocument2)
          list.Add(window.LocationName, windowDocument.body.outerHTML)
        End If
      End If
    Next window
    Return list
  End Function
 
End Class

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Bob Learned

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
mikesung99

ASKER
Thanks for all the useful Tips, using the InternetExporer document object we were able to figure out how to manipulate and enable the various controls that we require and produce our simple POC.
mikesung99

ASKER
Just to complete this thread - Here's our (very simple) code sample POC which demonstrates how to select an active browser (IExplorer) window, navigate to a specific URL and then fill in a specific field on the form and then initiate the submit button. This is an etxract from a VB .NET 2005 desktop application which has a text box and a command button to allow definition of the searchstring
Imports System
Imports System.Diagnostics
Imports System.Web
Imports System.Data
Imports System.Math
Imports System.Net
Imports System.Text
Imports System.IO
Imports Microsoft.Win32
Imports System.Runtime.InteropServices
Imports SHDocVw
Imports mshtml
Imports System.Threading
Imports MOZILLACONTROLLib
 
 
 
Public Class Form1
    Dim test100 As String
    Dim counter As Integer = 0
 
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        test100 = TextBox1.Text
 
        GetWindows(test100)
 
 
    End Sub
 
    Public Const MAX_PATH As Short = 260
    Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Integer, ByVal lParam As Integer) As Integer
    Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Public Const WM_CLOSE As Short = &H10S
    Public Const WM_LBUTTONUP As Integer = &H202
    Public Const WM_LBUTTONDOWN As Integer = &H201
    Public Const WM_RBUTTONDCLK As Integer = &H206
    Public Const WM_SETTEXT As Integer = &HC
 
 
    Public Function EnumFunc(ByVal hwnd As Integer, ByVal lpData As Integer) As Integer
        Dim lResult As Integer
        Dim sWndName As String
        Dim szClassName As String
 
        EnumFunc = 1
        szClassName = Space(MAX_PATH)
        sWndName = Space(MAX_PATH)
 
        lResult = GetClassName(hwnd, szClassName, MAX_PATH)
        szClassName = Microsoft.VisualBasic.Left(szClassName, lResult)
 
 
        lResult = GetWindowText(hwnd, sWndName, MAX_PATH)
        sWndName = Microsoft.VisualBasic.Left(sWndName, lResult)
 
        'Dim vreturnvalue As Object
        If InStr(sWndName, "Google - Windows") > 0 Then
            AppActivate(sWndName)
        End If
 
    End Function
 
    Public Delegate Function EnumFuncDeleg(ByVal hwnd As Integer, ByVal lpData As Integer) As Integer
 
    Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumFuncDeleg, ByVal lParam As Integer) As Integer
 
 
    Public Function fEnumWindows() As Boolean
        Dim hwnd As Integer
        Call EnumWindows(AddressOf EnumFunc, hwnd)
    End Function
    Public Shared Function GetWindows(test100 as String) As List(Of String)
        Dim list As New List(Of String)
        Dim test As String
        Dim testF As String
        Dim HandleIE As Integer
 
 
        For Each window As InternetExplorer In New ShellWindows()
            list.Add(window.LocationURL)
            test = window.LocationURL
            'MsgBox(window.LocationURL)
 
            If test = "http://www.google.co.uk/" Then
                MsgBox(window.LocationURL)
                window.Navigate("www.play.com")
                'Dim inp As Object
 
                Thread.Sleep(10000)
                HandleIE = window.HWND()
 
 
                window.Document.All("searchstring").Value = test100
                window.Document.All("go").Click()
 
 
 
 
            End If
        Next window
        Return list
    End Function
End Class

Open in new window