Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
the best way to do it is to look at the webbrowsers current URL when that event fires. Then you will know what URL the user is actually viewing, so your code can take appropriate action.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Originally Posted by SlumberMachine
Using your example code I've been able to do about 80% of a project I needed to create for work. But I have one last item to figure out.
The page has 4 drop down menus on it, each with about 20 or more items. I need to programatically search for a specific word and choose the drop down item that matches in each one. How would I do that? Here is what I've got so far:
Code:
Private Sub btnComboFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComboFill.Click
Dim MyHTMLCombo As mshtml.HTMLSelectElement = DirectCast(GetCurrentWebForm.item("ctl00_ManagementMain_FirmwareUserControl_FirmwareFilename"), mshtml.HTMLSelectElement)
Dim i As Integer
For i = 0 To MyHTMLCombo.size
If MyHTMLCombo.value.Contains(myfirmware) Then
MsgBox("I found it")
End If
Next
Well I solved this problem, here is the solution:
Code:
Dim MyHTMLCombo As mshtml.HTMLSelectElement = DirectCast(GetCurrentWebForm.item("ctl00_ManagementMain_FirmwareUserControl_FirmwareFilename"), mshtml.HTMLSelectElement)
Dim total As Integer
total = MyHTMLCombo.length
Dim i As Integer
For i = 0 To total
MyHTMLCombo.selectedIndex = i
If MyHTMLCombo.value.Contains("what im looking for") Then
found = True
Exit For
End If
Next
Yeah!
My Languages: English, VB 2005, Perl My Hobbies: Code, Photoshop, Skateboarding, Video Games, Flying, Cars(modification), Airbrushing, Electronic music DJing, and production.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
This article was awesome and I learned a ton, but I have come to something a little too much for me to figure out, i am trying to navigate to a url, select all of the checkboxes, and then run a javascript code that will download them. The issue is that the table with item names, numbers, and the checkboxes seem to be done on the serverside and then just pushed to the screen so i see no id's or names for any of the check boxes. There is an image thats click event selects them all but i cannot get it clicked. Here is the source code for it:
<td class='headerbar' nowrap width='4%' align='center' id=0><img OnClick='JobTable.MarkAll()' src='images/checker.gif' width=15 style='cursor:hand'></td>
plus i would like to know how to access the table, but one thing at a time. Thanks
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Better still, clicking that image fires a function:
function MarkAll()
I am trying this:
GetCurrentWebDoc.parentWindow.execScript("markall()", "javascript")
then you have to go the route of tracking down the image and clicking it. This is very easy if the image has an ID or NAME property to associate with it. Based on your HTML code above though, this image has neither, so the only thing you can do is loop the images, narrow down to the one you want, and then invoke its click method, something like this SHOULD work:
Code:
Dim MyImage As mshtml.HTMLImg = Nothing
For i As Integer = 0 To GetCurrentWebDoc.images.length - 1
MyImage = DirectCast(GetCurrentWebDoc.images.item(, i), mshtml.HTMLImg)
If MyImage.src.ToLower = "images/checker.gif" And MyImage.style.cursor = "hand" Then
MyImage.click()
Return
End If
Next
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Thanks, the first line worked like a charm. One more question, I have this code which will let me select a file, and gives info about that file, i would like to pass a row number and return the various values at the bottom of the code. Some thing like:
for i=1 to max rows
getrow(i).jobid.value
next
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
I read the whole thread and I can't find a solution for my problem.
What I'm doing wrong? If anyone can help me.
I want to submit username and password to this form http://iptv.bg/watch
but I can't get it work.
Here is my code (I'm using VB 6.0 and mshtml control):
Code:
Private Sub IPTVLogin()
Dim objMSHTML As New MSHTMLCtl.HTMLDocument
Dim objdoc As New MSHTMLCtl.HTMLDocument
Set objdoc = objMSHTML.createDocumentFromUrl("http://iptv.bg/watch", vbNullString)
While objdoc.readyState <> "complete"
DoEvents
Wend
If InStr(1, objdoc.body.innerHTML, "Вход") <> 0 Then
With objdoc
.All.Item("login").Value = "username"
.All.Item("password").Value = "password"
.Forms(0).submit
End With
End If
End Sub
and here is the some part of the code used in that web page (they use javascript for the login, I think)
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Any idea how to get the source code of the frame object? I can't seem to get this one.
Originally Posted by demausdauth
This was a fantastic help and very interesting to look through. Really enjoyed. Thanks kleinma.
Incidentally, this is what i ended up coding to access frames.
Code:
Private Function GetWebFrameObject(ByVal PassDocumentObject As Object) As mshtml.HTMLWindow2
Try
Return DirectCast(PassDocumentObject, mshtml.HTMLWindow2)
Catch ex As Exception
Return Nothing
End Try
End Function
Private Function getwebform(ByVal PassDocumentObject As mshtml.HTMLDocument) As mshtml.HTMLFormElement
Try
Return DirectCast(PassDocumentObject.forms.item(0), mshtml.HTMLFormElement)
Catch ex As Exception
Return Nothing
End Try
End Function
Private Sub SetFrameTextBox(ByVal FrameNumber As Integer, ByVal ValueToSendToBox As String, ByVal FieldToFill As String)
DirectCast(getwebform(GetWebFrameObject(GetCurrentWebDoc().frames.item(FrameNumber)).document).item(FieldToFill, 0), mshtml.HTMLInputElement).value = ValueToSendToBox
End Sub
Private Sub clicksubmitbuttonframes(ByVal FrameNumber As Integer, ByVal FieldToFill As String)
DirectCast(getwebform(GetWebFrameObject(GetCurrentWebDoc().frames.item(FrameNumber)).document).item(FieldToFill, 0), mshtml.HTMLButtonElement).click()
End Sub
The only thing that i didn't like about how I did this was I needed to know the exact frame index number.
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
I am trying to automate a process that involves repetitive access to a website after loggins on.
I have downloaded and reviewed the kleinma example and I am having trouble just getting started.
I am getting the error message
Message "Unable to cast object of type 'System.Windows.Forms.HtmlDocument' to type 'mshtml.HTMLDocument'." String
in the GetCurrentWebDoc function.
My code is just getting started but is posted below...
Public Class Form3
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
wb.Navigate("http://www.njmmis.com/login.aspx")
End Sub
'FILLS THE HTML TEXTBOX WITH TEXT FROM OUR WINFORM
Private Sub SetTextboxText(ByVal Text As String)
DirectCast(GetCurrentWebForm.item("txtBox"), mshtml.HTMLInputElement).value = Text
End Sub
'ALL THIS FUNCTION DOES IS GRABS THE CURRENT DOCUMENT FROM THE WEBBROWSER CONTROL
'AND CONVERTS IT TO THE STRONG TYPED mshtml.HTMLDocument CLASS
'THIS SAVES CODE BECAUSE YOU NEED TO GET THE WEB DOC FOR ALL CASES OF MANIPULATION
Private Function GetCurrentWebDoc() As mshtml.HTMLDocument
Try
Return DirectCast(wb.Document, mshtml.HTMLDocument)
Catch ex As Exception
Return Nothing
End Try
End Function
'SAME AS THE ABOVE FUNCTION, EXCEPT IT GRABS A WEBFORM. I ASSUME HERE THERE IS ONLY 1 FORM
'IF YOU WERE DEALING WITH A PAGE WITH MULTIPLE FORMS, SIMPLY USE ITS INDEX (THEY ARE IN AN ARRAY)
'THIS IS A USEFUL METHOD BECAUSE OFTEN TIMES WHEN MANIPULATING A WEBPAGE, ITS TO GET/SET DATA IN A FORM
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
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DirectCast(GetCurrentWebForm.item("txtUserName"), mshtml.HTMLInputElement).value = "xxxxxxxl"
End Sub
End Class
Any direction would be most appreciated.
I am using VS2005.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
My example code using the COM based webbrowser control. Prior to VS2005 there was no built in webbrowser control in the toolbox. So you had to use the COM one.
I am actually in the process of retooling this sample here to be exclusive with the 2005 managed browser control (well slightly extended) instead of the COM based one, but its going to have to wait until I finish up a few important projects.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
that error just means something is evaluating to null (nothing)
either GetCurrentWebDoc, documentElement, or outerHTML is equal to nothing when you call that. Figuring out which one, would be the first step to getting it working.
Since you didn't give the actual URL, I can't really advise any further.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
i would like to set both text displayed and the value. Is this even possible?
I would like to pass somekind array of parameters
something like
ParamName=value
so i was thinking that ParaName can be Value in combobox and the Value of param Could be the textdisplayed of combobox.Cause i only need this to pass data.
SO is there any other chances or possibilities to pass that kind of data, combobox would be great cause i have more than 1 parameter and i would not like to parse string in textbox
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
if you don't know the value, and only know the text you want to set, you need to loop through the "options" elements inside the select element until you find the one thats innertext value matches what you want, and then set that option as the selected option
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
i know both in my program paramName and value
Can you give me any example for VB6, on how to do that!
I acees to textbox like this WebBrowser1.document.GetElementById("txtPrintFormName").Value
so how can i access to combobox
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
it should be the same way, but using the combobox name instead of the textbox name.
I haven't coded in VB6 for a few years now, so I am not sure if there is anything additional to consider. As you probably know, this entire thread is focused on VB.NET
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Hi!
This is how it goes set the combobox in VB6
WebBrowser1.Document.GetElementById("DropDParam").Options(0).Text = "hmmmmmmmmmmmmmmmmmmmmmmmmmmm"
WebBrowser1.Document.GetElementById("DropDParam").Options(0).Value = "123"
But now i have another problem, when i click my go button on page
(code click event in my webpage)
String text = DropDParam.Items[0].Text;
it takes the default value from combobox, not the value i set in vb6 webbrowser control
i also must include this in web.config <pages enableEventValidation="false"/>
if i don't when i click on button it give me next error:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Hi i'm quite a noob, and what i want to do is extract a table from http://www.handelsblatt.com/News/def...ymbol=FLUK.NWX and save it as xls or txt. when i tried your project i recieved an error: " Name 'GetCurrentWebForm' is not declared ".
so why this error?
and where exactly do i find the code to extract?
and how do i save that to a file?
finally: how do i click a link ("weiter" at the bottom right of the table)
(hope i didnt ask something that has already been answered)
well, thanks in advance!
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
This has been a great learning tool.
Thanks a lot.
I also have the similar problem.
I want to enter the username and password into a form which is a Citrix MetraFrame XP page.
basically it's a Citrix page which gives access to some other box but we want to use Windows Credentials rather than citrix username/password. So what we are trying to do is, having a middle layer (vb.net app) which will load the citrix page behind the scenes. After authenticating with the windows credentials of the User, the page will fill in the citrix username/password and hit the login button.
I tried your code and the application you have provided (WebPageManipulation) but it falls over at GetCurrentWebForm function.
I investigated and found that Document Object doesn't have any forms in it or probably it's increpted. (i'm not sure) but the Document.Forms.Length always returns 0.
I have attached the file with this thread.
please take a look at it.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Hi Kleinma,
I've been looking through everything here and didn't see an answer to my question below, or I missed it somewhere.
I have a link I want to click but it has no id or tagname. It does have a 'href' and so in order to click it I want to try to loop through the links until I get my one using the code below.
Code:
For Each element As HtmlElement In WebBrowser.Document.Links
If element.GetAttribute("href").Contains("Call_Detail_Records") Then
element.InvokeMember("Click")
Exit For
End If
Next
Problem is that when it's looping it loops through the other links on the page but doesn't loop through the the three links in the centre of the page so it never reaches the call detail records link. By reading this forum my guess might be that it is in it's own form or something like that?
I am using IE Developer Toolbar to look at the webpages so that I can see what the id's, tag names etc are and it is very useful. But, it doesn't seem to allow me to identify forms, or at least I haven't figured that out yet. So, my question is, how can I know what forms are on the page? And secondly, why did the link I was looking for not get included in the WebBrowser.Document.Links object?
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
the fact that a given link may be inside a set of <form> tags should not exclude it from the links collection. However if the page has frames or some other structure to it, then that can cause the issue.
First thing to do is identify if infact this is a frameset page or not..
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Thanks, sorry I said forms earlier, but I meant frames... I'm not sure how to identify the frames on a page... (I am using IE Developer toolbar to identify all the other elements but can't see the frames).
If I right click on the area in IE and go to 'view source' I can of course see the html (below), but I don't see any frame tags exactly, there is a mention of myiframe, is this this the frame (what's the best way to idenify a frame - sorry I don't know much about this!)...
Code:
<html>
<head>
<title>Billing</title>
<META http-equiv='Content-Type' content='text/html; charset=UTF-8'>
<link rel="stylesheet" type="text/css" href="/ivechangedthis.css">
<script language='JavaScript'>
function dyniframesize()
{
if (document.getElementById)
{
//begin resizing iframe procedure
var dyniframe = window.parent.document.getElementById('myiframe');
var newHeight = 800;
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
the easiest way to determine actual frames is using the IE dev toolbar like you mention you have.
the webbrowsers document property is the highest level document in the page. In the case of a frames page, this document generally has little more than the outline of how each individual frame will be displayed (ie where it will show up) and also what the source URL is for each frame.
Being that each frame has its own URL, it also has its own document. So EACH frame contains a document object, and that is where the HTML you want to get at is.
Now if you know the framenumber (by index) or the frame name/id (if it has one) then you can access frames by those values. If not, you can also loop all frames, and inside that loop, loop all links in each frames document.
it would look like this:
Code:
'LOOP ALL FRAMES OF THE MAIN DOCUMENT
For Each frameWindow As HtmlWindow In WebBrowser1.Document.Window.Frames
'LOOP EACH LINK IN THE CURRENT FRAME
For Each element As HtmlElement In frameWindow.Document.Links
If element.GetAttribute("href").Contains("Call_Detail_Records") Then
element.InvokeMember("Click")
Exit For
End If
Next
Next
note that the exit for that is called only will exit the inner loop. You might want to put this code in its own subroutine so you can call exit sub once the link is found and clicked, to avoid the routine looking through the rest of the page when it does not have to.
See this screenshot. I found a random frames page example on the web, then using the IE dev toolbar, I can see that the main document is a frameset, and has 2 frames, and you can see each frame has its own document object which we can access.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
That's excellent. That worked. But why it worked is still a puzzle.
When I only use the code inner loop (For Each element As HtmlElement In WebBrowser.Document.Links) then, as I say, the three links in the centre of the page (which are perhaps in another frame) are skipped.
Now, when I include your outer loop and loop the frames (For Each frameWindow As HtmlWindow In WebBrowser.Document.Window.Frames) the three links in the centre of the page are now included in the inner loop BUT it looks as though there is only one frame because the outer loop only does one pass (there is no second framewindow to loop).
Does that make any sense to you? By looping the frames, the three central links were included (whereas before they weren't) but it still appears that there is only one frame as the outer loop only does 1 iteration. Why does the code work?
Comparing the html from the ie developer toolbar there is only
<HTML>
<HEAD>
<BODY>
and no mention of framesets, so I'm getting the impression there is only one frame on this webpage? Well, either way it works now so it's not a problem but I'd be interestd to know what's going on there?
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
even if there is only one frame, the page will still have the frameset page, and then the encapsulated frame which has the HTML you want to get at.
So it makes sense that it works with my outer loop even when there is only 1.
Without that loop of the frames pages, you are still only looking in the main document, which has nothing but information about the frame that will fill up the window.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
Ah ok great.
Can you help me with one final question, eh, well can I be greedy and ask for two!?
I remember reading somewhere here the mention of being able to gain control of a pop up window but don't remember seeing how to do it. I have a window which pops up and loads some text. I then want to save this as a .html file. Can I do it?
And also, I have scoured the web trying to find out how to do a 'save target as', you know, instead of simply clicking a link and bringing up an IE dialog box prompting whether you want to save or not, I would like to simulate a right click 'save target as' event so I can save straight onto my hard drive. Is it possible? I get the feeling it probably isn't?
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
kleinma:
I have tried your example and it seems to work in VB2008 but if I start from scratch and just copy in your code for a button to my project it doesn't work it seems to have something to do with the windows form genertated code. The error I get is on the 3 webBrowser.Document.forms.item(, 0) lines and the error is overload resolution failed because no accessible 'Item' accepts this number of arguments. How do I make it generate the windows form code for my own project?
Also I tried using a URL of https://www.harrahs.com/MyHarrahs.do with:
webBrowser.Document.forms.item(, 0).elements("accountId").value = "15701390709"
webBrowser.Document.forms.item(, 0).elements("pin").value = "1234"
webBrowser.Document.forms.item(, 0).submit()
but it comes back with a totally blank page instead of an error page, the ID and password are fake, or the correct status page with correct login info.
My code that generates the error is:
Imports System.Web
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate("https://www.harrahs.com/MyHarrahs.do")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Document.Forms.Item(, 0).elements("accountId").value = "1234567"
WebBrowser1.Document.Forms.Item(, 0).elements("pin").value = "1234"
WebBrowser1.Document.Forms.Item(, 0).submit()
End Sub
End Class
Last edited by ed08724; Feb 12th, 2008 at 05:30 PM.
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
here is my html sample
I'm trying to simulator automate filling of data to the webBrowser:
<html>
<head>
<form name="input" action="html_form_action.asp" method="get">
Username: <input type="text" name="user">
<br><input type="radio" name="sex" value="male"> Male
<br><input type="radio" name="sex" value="female"> Female
<br><input type="checkbox" name="vehicle" value="Bike">I have a bike
<br><input type="checkbox" name="vehicle" value="Car">I have a car
<br><input type="checkbox" name="vehicle" value="Airplane">I have an airplane
<br><input type="submit" value="Submit">
</form>
</head>
</html>
using vs2005, vb.net
FwebBrowser.Document.GetElementById("sex").SetAttribute("Gender", "male")
FwebBrowser.Document.GetElementById("vehicle").SetAttribute("Cartype", "Bike")
I had resolved text boxes, button click events, drop down list but coming to radio buttons and checkbox, I failed.
I couldn't understand how do I set the rdo btn o chkbox to "checked"
can someone guide me
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
I have a little bit of a problem. I did not see that it was covered previously, if it was I apologize.
I am attempting to create a password manager that automatically saves username and password information from a web page and fills it back in when you revisit the same web page. My dilemma is accurately finding the "User Name" element and "Password" element for each web page. Since every page can have different names, tag-names, etc.
I have been playing around with a couple of different methods, sometimes they work, with some pages that have multiple "input" elements and I cannot correctly identify the correct elements. My thought process so far has been to find an element on the page who’s type is "password", then find the element right before that that is a standard "input". Does anyone know if there is a way to get this information, and fill it back in accurately with any web page?
This is the source code that I am playing around with right now. Any ideas? Thanks.
For Each iHTMLEle In iHTMLCol
If Not LCase(iHTMLEle.type) = "hidden" Then
Debug.Print(iHTMLEle.name & " [" & iHTMLEle.type & "]")
Debug.Print(iHTMLEle.value)
Debug.Print("")
End If
Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control
I tried the paypal example and it worked fine with paypal but my form at https://www.harrahs.com/TotalRewards/Login does not have IDs only names. How can I get it to work w/o the IDs?
Thanks for any help.