|
-
Feb 7th, 2007, 03:36 AM
#1
Thread Starter
Hyperactive Member
[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
-
Feb 7th, 2007, 01:08 PM
#2
Frenzied Member
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.
-
Feb 7th, 2007, 08:02 PM
#3
Thread Starter
Hyperactive Member
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
-
Feb 8th, 2007, 11:47 AM
#4
Frenzied Member
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.
-
Feb 8th, 2007, 09:00 PM
#5
Thread Starter
Hyperactive Member
Re: Web Browser Control Problem
 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
-
Feb 8th, 2007, 10:56 PM
#6
Re: Web Browser Control Problem
You can do it without any Win32 API. See this thread.
With HTMLObject Library reference:
VB Code:
Option Explicit
Private Sub Form_Load()
WebBrowser1.Navigate2 "http://www.vbforums.com/"
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
'Wait till document has loaded
If pDisp Is WebBrowser1.Object Then
If WebBrowser1.LocationURL = "http://www.vbforums.com/" Then
Dim HTMLDOC As HTMLDocument
Dim HTMLA As HTMLAnchorElement
Set HTMLDOC = WebBrowser1.Document
'Loop through links
For Each HTMLA In HTMLDOC.links
If HTMLA.href = "http://www.vbforums.com/faq.php?" Then
MsgBox "Clicking on FAQ link"
HTMLA.Click
Exit Sub
End If
Next
End If
End If
End Sub
-
Feb 8th, 2007, 11:59 PM
#7
Thread Starter
Hyperactive Member
Re: Web Browser Control Problem
 Originally Posted by iPrank
You can do it without any Win32 API. See this thread.
With HTMLObject Library reference:
VB Code:
Option Explicit
Private Sub Form_Load()
WebBrowser1.Navigate2 "http://www.vbforums.com/"
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
'Wait till document has loaded
If pDisp Is WebBrowser1.Object Then
If WebBrowser1.LocationURL = "http://www.vbforums.com/" Then
Dim HTMLDOC As HTMLDocument
Dim HTMLA As HTMLAnchorElement
Set HTMLDOC = WebBrowser1.Document
'Loop through links
For Each HTMLA In HTMLDOC.links
If HTMLA.href = "http://www.vbforums.com/faq.php?" Then
MsgBox "Clicking on FAQ link"
HTMLA.Click
Exit Sub
End If
Next
End If
End If
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
-
Feb 9th, 2007, 01:18 AM
#8
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.
-
Feb 9th, 2007, 01:41 AM
#9
Thread Starter
Hyperactive Member
Re: Web Browser Control Problem
 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
-
Feb 14th, 2007, 07:00 PM
#10
Frenzied Member
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?
-
Feb 14th, 2007, 07:23 PM
#11
Thread Starter
Hyperactive Member
Re: Web Browser Control Problem
 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
-
Feb 15th, 2007, 12:22 PM
#12
Frenzied Member
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:
Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
'note: add reference of Microsoft.mshtml to your project
If AxWebBrowser1.LocationURL = "http://www.google.com/" Then 'notice that there is a "/" at the end of .com
Dim HTMLDOC As mshtml.HTMLDocument
Dim HTMLA As mshtml.HTMLAnchorElement
HTMLDOC = AxWebBrowser1.Document
'Loop through links
For Each HTMLA In HTMLDOC.links
If HTMLA.href = "http://www.google.com/preferences?hl=en" Then
MsgBox("Clicking on preference link.")
HTMLA.click()
Exit Sub
End If
Next
End If
End Sub
You could also do a similar thing with the textbox inputs. The code below:
VB Code:
Dim HTMLDOC As mshtml.HTMLDocument
HTMLDOC = AxWebBrowser1.Document 'make sure axwebbrowser1 already navigated to a page before going to this code
Dim inner As String = HTMLDOC.body.innerHTML
Dim replaceStr As String = Chr(34) & "Google Search" & Chr(34) '= "Google Search" -- chr(34) is apostrophe
Dim newStr As String = replaceStr & " value=" & Chr(34) & "Hello World" & Chr(34)
inner = inner.Replace(replaceStr, newStr)
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.
-
Feb 15th, 2007, 07:40 PM
#13
Thread Starter
Hyperactive Member
Re: Web Browser Control Problem
 Originally Posted by benmartin101
I think you should just use what IPrank suggested with the button click. Here is the .NET equivalent:
VB Code:
Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
'note: add reference of Microsoft.mshtml to your project
If AxWebBrowser1.LocationURL = "http://www.google.com/" Then 'notice that there is a "/" at the end of .com
Dim HTMLDOC As mshtml.HTMLDocument
Dim HTMLA As mshtml.HTMLAnchorElement
HTMLDOC = AxWebBrowser1.Document
'Loop through links
For Each HTMLA In HTMLDOC.links
If HTMLA.href = "http://www.google.com/preferences?hl=en" Then
MsgBox("Clicking on preference link.")
HTMLA.click()
Exit Sub
End If
Next
End If
End Sub
You could also do a similar thing with the textbox inputs. The code below:
VB Code:
Dim HTMLDOC As mshtml.HTMLDocument
HTMLDOC = AxWebBrowser1.Document 'make sure axwebbrowser1 already navigated to a page before going to this code
Dim inner As String = HTMLDOC.body.innerHTML
Dim replaceStr As String = Chr(34) & "Google Search" & Chr(34) '= "Google Search" -- chr(34) is apostrophe
Dim newStr As String = replaceStr & " value=" & Chr(34) & "Hello World" & Chr(34)
inner = inner.Replace(replaceStr, newStr)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|