Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Originally Posted by kleinma
instead of using the form specifically, try Invoking a click on the submit button using its ID of "uploadButton"
It worked!
but you know what somewhere in my tests im sure i did that, ive been working with it for 4 hours and its almost 2am, now, how do I get rid of ghosts inside this program, hmmm... lesser errors to go! weeeeeeee
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
I'm attempting to
1) click a single checkbox on the loaded page (which should redirect the page in the process)
2) click a URL on the next page
Apparrently the checkbox is beyond my scope because I can't seem to get it to work. There are 5 forms on the page, and the check box is in the 4th (index 3?). Also in that form is a hidden value which is specified before the checkbox name. Here is some code I'm working with:
Code:
Public Class frmMain
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tmpStr As String = "http://localhost/CASE_NUMBER=" & CStr(TextBox1.Text)
wb.Url = New System.Uri(tmpStr)
End Sub
Private Function GetCurrentWebDoc() As MSHTML.HTMLDocument
Try
Return DirectCast(wb.Document, mshtml.HTMLDocument)
Catch ex As Exception
Return Nothing
End Try
End Function
Private Function GetCurrentWebForm() As MSHTML.HTMLFormElement
Try
If GetCurrentWebDoc.forms.length > 0 Then
Return DirectCast(GetCurrentWebDoc.forms.item(3), MSHTML.HTMLFormElement)
Else
Return Nothing
End If
Catch ex As Exception
Return Nothing
End Try
End Function
Private Sub ClickCheckBox()
Dim MyInputElement As MSHTML.HTMLInputElement = DirectCast(GetCurrentWebForm.item("checkBoxName", 3), MSHTML.HTMLInputElement)
MyInputElement.checked = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ClickCheckBox()
End Sub
End Class
Unfortunately I can't really post the HTML I'm working with, but I could cut and past relevant portions if I knew what would help.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
if you look at my example project you will see an example of calling javascript that is in the HTML page from VB code. You could use that example to call the signInSubmit javascript function from VB code, passing the given values that it expects. That should do it.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Originally Posted by kleinma
if you look at my example project you will see an example of calling javascript that is in the HTML page from VB code. You could use that example to call the signInSubmit javascript function from VB code, passing the given values that it expects. That should do it.
I followed the script on your sample application, would there be reasons why my javascript function wont execute? Nothing is happening, I am now trying to click a link with a js function to turn off a flash part on the site so that I could write on the text box
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Originally Posted by knds
I followed the script on your sample application, would there be reasons why my javascript function wont execute? Nothing is happening, I am now trying to click a link with a js function to turn off a flash part on the site so that I could write on the text box
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
I am not sure why it would not execute. It would be hard for me to guess without being able to see the actual page you are trying to manipulate. Is it a public URL so I can look at it?
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
if you are using my sample code, then you could use the GetCurrentWebDoc to get the current webpage document, and then grab whatever form you want by name or by its index in the collection
Code:
Dim MyForm As mshtml.HTMLFormElement = DirectCast(GetCurrentWebDoc.forms.item("form name here"), mshtml.HTMLFormElement)
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
yes in the getcurrentwebform method in my example code, you will see it is specifically grabbing form at index 0. Just change that 0 to whatever number index you want to grab. You could also change the function to allow you to pass in an integer as a parameter, and use that to indicate which form you actually want to retrieve from the webpage document.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
No problem.
Check back soon, I will be releasing source for an extended WB control that uses the .NET 2.0 built in webbrowser control instead of the COM based one, but has all the features that the 2 provide.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
when you run into this type of situation, try to figure out what action you are trying to simulate versus the result you want (sometimes the opposite is true). In this case, you want to simulate clicking on an image. It just so happens that the result is submitting the form.
Try this code to simulate clicking the image, which in turn should fire any code attached to that image.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Originally Posted by kleinma
when you run into this type of situation, try to figure out what action you are trying to simulate versus the result you want (sometimes the opposite is true). In this case, you want to simulate clicking on an image. It just so happens that the result is submitting the form.
Try this code to simulate clicking the image, which in turn should fire any code attached to that image.
if that name value doesn't work, try the ID instead ("ctl00_Main_postComment_postcommentImageButton")
I do hope you are making some sort of "utility", and not anything that could be used maliciously to spam comments all over myspace.
What a great resource this thread has been, one of very few on the internet for this subject matter. Specific to this issue, my company now has it's own myspace page and it's my job to work it. %-(
I'm first seeing if it's feasible to do by generating this temporary test solution. I need to auto-comment anyone who sends us a comment or becomes our friend. Akin to this post, you showed how to click on an image, but I'm trying to fill in a textarea form field with your example. However, I'm running into problems. Here is the text area field data source I'm trying to automate similar to the poster above:
I used your source project to try and autofill the textarea box but it's not working. My test program throws an exception stating "Object reference not set to an instance of an object") It keeps telling me that I need to declare a new object. I've tried both ID & Name of control and here is a sample code I'm trying:
Code:
Public Class BuiltInBrowser
Private Sub BuiltInBrowser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
wB.Navigate("http://comment.myspace.com/index.cfm?fuseaction=user.viewProfile_commentForm&friendID=8")
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetText.Click
SetTextareaText(txtMessage.Text)
End Sub
Private Sub SetTextareaText(ByVal Text As String)
Try
DirectCast(GetCurrentWebForm.item("ctl00cpMain$postComment$commentTextBox"), mshtml.HTMLTextAreaElement).value = Text
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Function GetCurrentWebDoc() As mshtml.HTMLDocument
Try
Return DirectCast(wb.Document, mshtml.HTMLDocument)
Catch ex As Exception
Return Nothing
End Try
End Function
Private Function GetCurrentWebForm() As mshtml.HTMLFormElement
Try
If GetCurrentWebDoc.forms.length > 0 Then
Return DirectCast(GetCurrentWebDoc.forms.item(0), mshtml.HTMLFormElement)
Else
Return Nothing
End If
Catch ex As Exception
Return Nothing
End Try
End Function
End Class
Any help in this matter would be GREATLY appreciated!
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Hi kleinma,
This site is the best source for web page manipulation using GetCurrentWebDoc. I have been using your code to manipulate few sites. But some how I can't access any links, frame or any type of tag for this (http://www.insidercow.com/) site. I get the counts as "0" for all the tags.
I am using a similar code like below.
Dim otr As mshtml.IHTMLElementCollection = GetCurrentWebDoc.getElementsByTagName("table")
But a web page related to the above web site (http://206.222.29.162/winners.jsp), I do lot of manipulation using your example codes.
Advance thanks.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Originally Posted by kumartech
Hi kleinma,
This site is the best source for web page manipulation using GetCurrentWebDoc. I have been using your code to manipulate few sites. But some how I can't access any links, frame or any type of tag for this (http://www.insidercow.com/) site. I get the counts as "0" for all the tags.
I am using a similar code like below.
Dim otr As mshtml.IHTMLElementCollection = GetCurrentWebDoc.getElementsByTagName("table")
But a web page related to the above web site (http://206.222.29.162/winners.jsp), I do lot of manipulation using your example codes.
Advance thanks.
Kumartech,
that site is a bit odd, in that it uses a single frame page. So getting the current web doc using the function in my code simply returns the frames page, and not the frame document itself. So you can use this code, to grab the frame, and then grab the document from that frame.
Code:
Dim mainFrame As mshtml.HTMLWindow2 = DirectCast(GetCurrentWebDoc.frames.item(0), mshtml.HTMLWindow2)
Dim CurrentWebDoc As mshtml.HTMLDocument = DirectCast(mainFrame.document, mshtml.HTMLDocument)
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Hai,we would like thanks to kleinma that provide sample code to us for make our development more easy.But,we are having some issue about button.We have found most button at html pages that available at internet nowadays.Their don't provide id or name for button.At sample code,it only work for button when have id or name.Please refer following for the html code(submit button).
<form method="GET" action="http://www.myweb.com/addurl.html">
<tr><td align=left><font face="helvetica,arial,sans-serif">
Url to add:<br>
<input type="text" name="url" size=30 value="http://"><br><br>
Your email address:<br>
<input type="text" name="email" size=30 value=""><br>
<input type="checkbox" name="optin" checked value="1"> Send me the newsletter<br><br>
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Originally Posted by gmchun83
Hai,we would like thanks to kleinma that provide sample code to us for make our development more easy.
But,we are having some issue about button.We have found most button at html pages that available at internet nowadays.
Their don't provide id or name for button.
At sample code,it only work for button when have id or name.Please refer following for the html code(submit button).
if i understand you correct then you can try this
Code:
Dim theWElementCollection2 As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theWElementCollection
Dim controlName As String = curElement.GetAttribute("type").ToString
If controlName = "submit" Then
curElement.InvokeMember("click")
End If
Next
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Sorry, I misread it as just an image (as in <img> tag) and not an input element of type image.
For some reason, I can't seem to access the specific element when its type image via the forms item collection. However this code seems to work:
Code:
Dim MyImageElement As mshtml.HTMLInputElement = DirectCast(GetCurrentWebDoc.all.item("ctl00_Main_postComment_postcommentImageButton"), mshtml.HTMLInputElement)
MyImageElement.click()
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Originally Posted by kleinma
Sorry, I misread it as just an image (as in <img> tag) and not an input element of type image.
For some reason, I can't seem to access the specific element when its type image via the forms item collection. However this code seems to work:
kleinma, first Id like to say a big thanks for this thread, it has been invaluable to me.
However, I had a page input button I wanted to click with no ID or Name along with several other input buttons on the page that I didnt want to click. To get round this I identified the button by its .src
heres the code..
Code:
Dim thisObj As Object
Dim butInput As mshtml.HTMLInputElement
thisObj = AxWebBrowser1.Document.getElementsByTagName("input")
For Each butInput In thisObj
If butInput.src = "TheNameOfTheImage.gif" Then
butInput.click()
End If
Next
If you or anyone has a better way of doing it Id be interested to know, but this worked for me and maybe it'll help someone else too.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Hi, I am also trying to code using VB6 to pass values from my application and fill the values in the web pages and submit the page. But the web pages are in frames. I am new to all these...
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Off the top of my head I can't think of any other way of doing it. It does become a bit trickier when you have to deal with HTML that doesn't provide all the tags that it should (like a name or ID tag).
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
noo.. i mean automatically click on a submit button... because i'm having a problem while using the navigate2 function of axshdocvw...and i'm getting the same error when i use the InvokeMember("click") function of the original webbrowser provided in VB2005 expresss ed. here is the error screenshot:
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
noo...i've no page loaded yet..i'm loading it when the app starts...
look..i got a webform, which requires some dates and to be submitted...and i want the resulted html code...now i want it to be done multiple time with different dates..like
for i as integer = 0 to 100
' fill the date in the webform
' submit the form
' get the resulted html
' parse the html code
next
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
you can't quite do it in a loop like that, because the webbrowser is event driven. It fires events when it is doing things so you know what state it is in, and you know when you should proceed.
So to accomplish a repetitive task like that, you would need to do something like the following:
Lets say you have page1.htm (which is the page that has the fields that need to be filled in), and page2.htm (which is the page the form submits to and has the resulting HTML code you want)
Code:
-Navigate to page1.htm
-When the DocumentCompleted event of the webbrowser fires the page has fully loaded
check the current URL of the WB control
if its page1.htm, fill in the values you want and submit form
if its page2.htm, then page1 was just submitted, so get the resulting HTML code.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
yeah i have done the same thing in the original browser but i have to click to button each time... the code is something like this:
Code:
dim counter as integer = 0
prv sub form_onload(...)
webbrowser1.documenttext = mycode
' press submit button code should be here
end sub
prv sub webbrowser1_documentcomplete(...)
if webbrowser1.documenttitle = submitted_page_title then
parse(webbrowser1.documenttext)
if counter <> 101 then
webbrowser1.documenttext = mycode
' press submit button code should be here
end if
end if
end sub
prv sub parse(html as string)
' parse html
counter += 1
end sub
i just want the button to be clicked automatically without making a BIG change..i think you understand
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Hi kleinma,
I have been following this post for couple of days. I would like to mention that I have never seen anyone so HELPFUL.. You rock!!
well, i got a problem in doing the same thing. I am using Vb.net 2005 express edition, wherein i am creating an application, that would send in data to the web form fields and get the results. It's actually a ASP.NET webpage, that calculates based on the values entered in the fields.
I don't know why Mmhtml code, didn't work for me, so i am working with the web browser control 2.0, with the following code:
PROBLEM: after I execute this code, the web browser loops it, making the page to reload, input the variables and again click it.. it forms a vicious circle..