Results 1 to 13 of 13

Thread: [RESOLVED] Web Browser Control Problem

  1. #1

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Resolved [RESOLVED] Web Browser Control Problem

    Hi guys!

    Got a problem here.

    How do you emulate a mouse click button through code. (I hope the question is clear.)

    Thanks in advance guys.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Web Browser Control Problem

    post a specific window message to the form. You'll need the window handle to the form that you're trying to emulate the mouse click. The api functions you'll need would be PostMessage() and FindWindow()/FindWindowEx(). You'll be sending WM_LBUTTONDOWN and WM_LBUTTONUP. To learn more about these functions and constants, and see examples of how they are use, you could do a search at the API forum.

  3. #3

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Web Browser Control Problem

    Thanks BenMartin,

    Ok. Here is what I have got so far.

    Code:
        Private Const WM_LBUTTONDOWN = &H201
        Private Const WM_LBUTTONUP = &H202
        Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName _
            As String, ByVal lpWindowName As String) As Long
        Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As _
            Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Long) As Long
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AxWebBrowser1.Navigate("http://somewebsite.com")
        End Sub
    
        Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
        End Sub
    What I am planning to do is click on the link in the page. I have used the Web Browser Control.

    What I should do next is to use FindWindow and store it in a long data type named lhwnd and use Postmessage. Is that it?
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  4. #4
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Web Browser Control Problem

    If your using vb6, then it would be LONG data type, if its VB.NET, then it would be INTEGER data type. Since your using Web Browser Control, you probably don't need findwindow(), just use the ".handle" property of your web browser control. Then PostMessage() the WM_LBUTTONDOWN and/or WM_LBUTTONUP. However, you'll need to find the coordinates/location of where you want to send the mouse click.

  5. #5

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Web Browser Control Problem

    Quote Originally Posted by benmartin101
    If your using vb6, then it would be LONG data type, if its VB.NET, then it would be INTEGER data type. Since your using Web Browser Control, you probably don't need findwindow(), just use the ".handle" property of your web browser control. Then PostMessage() the WM_LBUTTONDOWN and/or WM_LBUTTONUP. However, you'll need to find the coordinates/location of where you want to send the mouse click.
    Thanks BenMartin,

    Here's what I have got so far.

    Code:
        Private Const WM_LBUTTONDOWN = &H201
        Private Const WM_LBUTTONUP = &H202
        Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As _
            Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As _
            Integer
        Private Structure POINTAPI
            Dim X As Long
        End Structure
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AxWebBrowser1.Navigate("http://somewebsite.com")
        End Sub
    
        Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
            PostMessage(AxWebBrowser1.Handle.ToInt32, WM_LBUTTONDOWN, 1, 0&)
            PostMessage(AxWebBrowser1.Handle.ToInt32, WM_LBUTTONUP, 1, 0&)
        End Sub
    PostMessage now is working (I tested it on a command button.). Thanks to you.

    I will now work on finding the coordinates of the link button. After finding it, I will store it in a structure and add it in the PostMessage function. Is this correct?
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  6. #6
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Web Browser Control Problem

    You can do it without any Win32 API. See this thread.

    With HTMLObject Library reference:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     WebBrowser1.Navigate2 "http://www.vbforums.com/"
    5. End Sub
    6.  
    7. Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    8.     'Wait till document has loaded
    9.     If pDisp Is WebBrowser1.Object Then
    10.         If WebBrowser1.LocationURL = "http://www.vbforums.com/" Then
    11.             Dim HTMLDOC As HTMLDocument
    12.             Dim HTMLA As HTMLAnchorElement
    13.             Set HTMLDOC = WebBrowser1.Document
    14.        
    15.             'Loop through links
    16.             For Each HTMLA In HTMLDOC.links
    17.                 If HTMLA.href = "http://www.vbforums.com/faq.php?" Then
    18.                     MsgBox "Clicking on FAQ link"
    19.                     HTMLA.Click
    20.                     Exit Sub
    21.                 End If
    22.             Next
    23.         End If
    24.     End If
    25. End Sub
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  7. #7

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Web Browser Control Problem

    Quote Originally Posted by iPrank
    You can do it without any Win32 API. See this thread.

    With HTMLObject Library reference:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     WebBrowser1.Navigate2 "http://www.vbforums.com/"
    5. End Sub
    6.  
    7. Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    8.     'Wait till document has loaded
    9.     If pDisp Is WebBrowser1.Object Then
    10.         If WebBrowser1.LocationURL = "http://www.vbforums.com/" Then
    11.             Dim HTMLDOC As HTMLDocument
    12.             Dim HTMLA As HTMLAnchorElement
    13.             Set HTMLDOC = WebBrowser1.Document
    14.        
    15.             'Loop through links
    16.             For Each HTMLA In HTMLDOC.links
    17.                 If HTMLA.href = "http://www.vbforums.com/faq.php?" Then
    18.                     MsgBox "Clicking on FAQ link"
    19.                     HTMLA.Click
    20.                     Exit Sub
    21.                 End If
    22.             Next
    23.         End If
    24.     End If
    25. End Sub
    Hi iPrank,

    Thanks for the reply, I will also use that and the API that benmartin suggested because the link opens a new pop-up window.

    Do you have another example that is in VB .NET 2003 format. The event parameters in the WebBrowser_DocumentComplete Event is different than in Vb 6.0.

    Therefore I cannot identify properly what to do with pDisp in VB .NET 2003
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  8. #8
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Web Browser Control Problem

    Sorry. I don't know VB.Net.
    Make a VB6 project and try to import it and see what happens. If that doesn't work, post in VB.NET section.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  9. #9

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Web Browser Control Problem

    Quote Originally Posted by iPrank
    Sorry. I don't know VB.Net.
    Make a VB6 project and try to import it and see what happens. If that doesn't work, post in VB.NET section.
    I already did. But alas, jm only responded and was not resolved.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  10. #10
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Web Browser Control Problem

    What do you need to do with the pop up window? Close it or do you need to do what u did with the first one?

  11. #11

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Web Browser Control Problem

    Quote Originally Posted by benmartin101
    What do you need to do with the pop up window? Close it or do you need to do what u did with the first one?
    Hi benmartin,

    I will require the API to transact with the pop-up window. At the first page, I need to fill up 2 textboxes, one is a textbox and another is a password.

    By the way, how will you know the location of the mouse coordinates. I can't seem to locate it so I can't click on the link?

    Can this be done by PostMessage alone or I will require another API function call?
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  12. #12
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Web Browser Control Problem

    I think you should just use what IPrank suggested with the button click. Here is the .NET equivalent:

    VB Code:
    1. Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
    2.         'note: add reference of Microsoft.mshtml to your project
    3.         If AxWebBrowser1.LocationURL = "http://www.google.com/" Then 'notice that there is a "/" at the end of .com
    4.             Dim HTMLDOC As mshtml.HTMLDocument
    5.             Dim HTMLA As mshtml.HTMLAnchorElement
    6.             HTMLDOC = AxWebBrowser1.Document
    7.  
    8.             'Loop through links
    9.             For Each HTMLA In HTMLDOC.links
    10.                 If HTMLA.href = "http://www.google.com/preferences?hl=en" Then
    11.                     MsgBox("Clicking on preference link.")
    12.                     HTMLA.click()
    13.  
    14.                     Exit Sub
    15.                 End If
    16.             Next
    17.         End If
    18.     End Sub

    You could also do a similar thing with the textbox inputs. The code below:

    VB Code:
    1. Dim HTMLDOC As mshtml.HTMLDocument
    2.         HTMLDOC = AxWebBrowser1.Document 'make sure axwebbrowser1 already navigated to a page before going to this code
    3.         Dim inner As String = HTMLDOC.body.innerHTML
    4.  
    5.         Dim replaceStr As String = Chr(34) & "Google Search" & Chr(34) '= "Google Search" -- chr(34) is apostrophe
    6.         Dim newStr As String = replaceStr & " value=" & Chr(34) & "Hello World" & Chr(34)
    7.         inner = inner.Replace(replaceStr, newStr)
    8.         HTMLDOC.body.innerHTML = inner

    Try the above sample with Google.com, it should put a "Hello World" on the search textbox. What it's basically doing is changing the html code to that of a google page that has "Hello World" in the textbox. You can find out which html codes to replace by going to the target html page, putting your input on the textbox and doing a View->Source on internet explorer. You'll see that your input is somewhere in the html source code.
    Last edited by benmartin101; Feb 15th, 2007 at 12:26 PM.

  13. #13

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Web Browser Control Problem

    Quote Originally Posted by benmartin101
    I think you should just use what IPrank suggested with the button click. Here is the .NET equivalent:

    VB Code:
    1. Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
    2.         'note: add reference of Microsoft.mshtml to your project
    3.         If AxWebBrowser1.LocationURL = "http://www.google.com/" Then 'notice that there is a "/" at the end of .com
    4.             Dim HTMLDOC As mshtml.HTMLDocument
    5.             Dim HTMLA As mshtml.HTMLAnchorElement
    6.             HTMLDOC = AxWebBrowser1.Document
    7.  
    8.             'Loop through links
    9.             For Each HTMLA In HTMLDOC.links
    10.                 If HTMLA.href = "http://www.google.com/preferences?hl=en" Then
    11.                     MsgBox("Clicking on preference link.")
    12.                     HTMLA.click()
    13.  
    14.                     Exit Sub
    15.                 End If
    16.             Next
    17.         End If
    18.     End Sub

    You could also do a similar thing with the textbox inputs. The code below:

    VB Code:
    1. Dim HTMLDOC As mshtml.HTMLDocument
    2.         HTMLDOC = AxWebBrowser1.Document 'make sure axwebbrowser1 already navigated to a page before going to this code
    3.         Dim inner As String = HTMLDOC.body.innerHTML
    4.  
    5.         Dim replaceStr As String = Chr(34) & "Google Search" & Chr(34) '= "Google Search" -- chr(34) is apostrophe
    6.         Dim newStr As String = replaceStr & " value=" & Chr(34) & "Hello World" & Chr(34)
    7.         inner = inner.Replace(replaceStr, newStr)
    8.         HTMLDOC.body.innerHTML = inner

    Try the above sample with Google.com, it should put a "Hello World" on the search textbox. What it's basically doing is changing the html code to that of a google page that has "Hello World" in the textbox. You can find out which html codes to replace by going to the target html page, putting your input on the textbox and doing a View->Source on internet explorer. You'll see that your input is somewhere in the html source code.
    WOW!!!! Cool Thanks.

    + reputation to you.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width