Reading and writing data to/from HTML Documents

darbid

Registered User.
Local time
Today, 06:32
Joined
Jun 26, 2008
Messages
1,428
I am about to put a web browser on a form and try to automate the filling out of text boxes, reading of fields and clicking of buttons on a website I have no control over.

Where do i start to learn this? I know that I have to use MSHTML and I get this by setting a HTMLDOCUMENT = Me.Webbrowser.document.

But how do I learn how to find the items/elements blah blah in the html pages? Anyone got some good online resources?
 
Here's some code that might give you a start...
Code:
[FONT="Verdana"][SIZE="1"]Private WithEvents m_wb As SHDocVw.InternetExplorer

Private Sub Form_Load()
   Set m_wb = Me.WebBrowser0.Object
   m_wb.Navigate2 "http://www.nhl.com/"
End Sub

Private Sub m_wb_DocumentComplete(ByVal pDisp As Object, URL As Variant)
   Debug.Print URL
   Select Case URL
      Case "http://www.nhl.com/"
         Dim obj As Object
         MsgBox "set a breakpoint here, and check out the properties of the pDisp object in the locals window" & vbCrLf & _
            "you can navigate the whole object tree of the document."
         For Each obj In pDisp.Document.all
            Debug.Print obj.id, obj.innerhtml
         Next
   End Select
End Sub[/SIZE][/FONT]
 
Thanks that did get me started.

as a beginner my favourite is
Code:
document.documentElement.outerHTML
which shows me the whole document

As an example for me what is the best way to get one of these menu elements to click on them.
Code:
<BODY bgColor=#bfcfdf>
<DIV id=menu_1 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; LEFT: 10px; VISIBILITY: visible; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; CURSOR: hand; POSITION: absolute; TOP: 4px; BORDER-RIGHT-WIDTH: 0px"><A onmouseover="if (parent.frames[3].clickenable != 0) { mb_menuover(1); popUp('elMenu1',event); } else { mb_clearmenu(); }" onclick="self.focus(); return false;" onmouseout="if (parent.frames[3].clickenable != 0) { mb_menuout(1); popDown('elMenu1'); } else { mb_clearmenu(); }" href="javascript://"><SPAN class=menu_text><NOBR> Patente </NOBR></SPAN></A></DIV>
<DIV id=menu_2 style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; LEFT: 80px; VISIBILITY: visible; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 1px; CURSOR: hand; POSITION: absolute; TOP: 4px; BORDER-RIGHT-WIDTH: 0px"><A onmouseover="if (parent.frames[3].clickenable != 0) { mb_menuover(2); popUp('elMenu2',event); } else { mb_clearmenu(); }" onclick="self.focus(); return false;" onmouseout="if (parent.frames[3].clickenable != 0) { mb_menuout(2); popDown('elMenu2'); } else { mb_clearmenu(); }" href="javascript://"><SPAN class=menu_text><NOBR> Marken </NOBR></SPAN></A></DIV>
<DIV id=line_2 style="LEFT: 77px; VISIBILITY: visible; WIDTH: 1px; POSITION: absolute; TOP: 4px"><IMG height=16 src="line3d.gif" width=2></DIV>
<DIV id=menu_3 style="BORDER-RIGHT: #ffffff 0px inset; BORDER-TOP: #ffffff 0px inset; LEFT: 143px; VISIBILITY: visible; BORDER-LEFT: #ffffff 0px inset; WIDTH: 1px; CURSOR: hand; BORDER-BOTTOM: #ffffff 0px inset; POSITION: absolute; TOP: 4px; BACKGROUND-COLOR: #bfcfdf"><A onmouseover="if (parent.frames[3].clickenable != 0) { mb_menuover(3); popUp('elMenu3',event); } else { mb_clearmenu(); }" onclick="self.focus(); return false;" onmouseout="if (parent.frames[3].clickenable != 0) { mb_menuout(3); popDown('elMenu3'); } else { mb_clearmenu(); }" href="javascript://"><SPAN class=menu_text><NOBR menuName="elMenu3"> Fristen </NOBR></SPAN></A></DIV>
<DIV id=line_3 style="LEFT: 140px; VISIBILITY: visible; WIDTH: 1px; POSITION: absolute; TOP: 4px"><IMG height=16 src="line3d.gif" width=2></DIV>
<DIV id=menu_4 style="BORDER-RIGHT: #e0e0e0 0px outset; BORDER-TOP: #e0e0e0 0px outset; LEFT: 213px; VISIBILITY: visible; BORDER-LEFT: #e0e0e0 0px outset; WIDTH: 1px; CURSOR: hand; BORDER-BOTTOM: #e0e0e0 0px outset; POSITION: absolute; TOP: 4px; BACKGROUND-COLOR: #bfcfdf"><A onmouseover="if (parent.frames[3].clickenable != 0) { mb_menuover(4); popUp('elMenu4',event); } else { mb_clearmenu(); }" onclick="self.focus(); return false;" onmouseout="if (parent.frames[3].clickenable != 0) { mb_menuout(4); popDown('elMenu4'); } else { mb_clearmenu(); }" href="javascript://"><SPAN class=menu_text><NOBR menuName="elMenu4"> Gemeinsame Fkt. </NOBR></SPAN></A></DIV>
<DIV id=line_4 style="LEFT: 210px; VISIBILITY: visible; WIDTH: 1px; POSITION: absolute; TOP: 4px"><IMG height=16 src="line3d.gif" width=2></DIV>
<DIV id=menu_5 style="BORDER-RIGHT: #e0e0e0 0px outset; BORDER-TOP: #e0e0e0 0px outset; LEFT: 339px; VISIBILITY: visible; BORDER-LEFT: #e0e0e0 0px outset; WIDTH: 1px; CURSOR: hand; BORDER-BOTTOM: #e0e0e0 0px outset; POSITION: absolute; TOP: 4px; BACKGROUND-COLOR: #bfcfdf"><A onmouseover="if (parent.frames[3].clickenable != 0) { mb_menuover(5); popUp('elMenu5',event); } else { mb_clearmenu(); }" onclick="self.focus(); return false;" onmouseout="if (parent.frames[3].clickenable != 0) { mb_menuout(5); popDown('elMenu5'); } else { mb_clearmenu(); }" href="javascript://"><SPAN class=menu_text><NOBR menuName="elMenu5"> Gebühren </NOBR></SPAN></A></DIV>
<DIV id=line_5 style="LEFT: 336px; VISIBILITY: visible; WIDTH: 1px; POSITION: absolute; TOP: 4px"><IMG height=16 src="line3d.gif" width=2></DIV>
<DIV id=menu_6 style="BORDER-RIGHT: #ffffff 0px inset; BORDER-TOP: #ffffff 0px inset; LEFT: 416px; VISIBILITY: visible; BORDER-LEFT: #ffffff 0px inset; WIDTH: 1px; CURSOR: hand; BORDER-BOTTOM: #ffffff 0px inset; POSITION: absolute; TOP: 4px; BACKGROUND-COLOR: #bfcfdf"><A onmouseover="if (parent.frames[3].clickenable != 0) { mb_menuover(6); popUp('elMenu6',event); } else { mb_clearmenu(); }" onclick="self.focus(); return false;" onmouseout="if (parent.frames[3].clickenable != 0) { mb_menuout(6); popDown('elMenu6'); } else { mb_clearmenu(); }" href="javascript://"><SPAN class=menu_text><NOBR menuName="elMenu6"> Infoversorgung </NOBR></SPAN></A></DIV>
<DIV id=line_6 style="LEFT: 413px; VISIBILITY: visible; WIDTH: 1px; POSITION: absolute; TOP: 4px"><IMG height=16 src="line3d.gif" width=2></DIV>
<SCRIPT language=JavaScript type=text/javascript>
        // set menu load status to 1 to indicate that frame
 
The reason for my above question is that I was not getting a reliable result. I have seen through hours of testing that for example the below code upon entry into the web page has NULL as the onclick. But if I manually move my mouse over the menu item and then check this onclick again it now has a big long script as the onclick.

This means that if i just try to use the DOM to click this nothing happens.
If I put my mouse over the menu then use the DOm to click this item all is good.
Thus it needs human interaction first.

Could someone at least give me,

1. some pointers on how this kind of things works ie. mouseover then gives it an onclick event - it must download a ???? File, url, form, frame what does it do?
2. maybe offer some assistance on how to use the DOM to make this mouseover work
3. understand what the onclick actually calls (ie the Jscript) so that I may be able to skip using the DOM to click the element and just call the resulting events.
Code:
<A onmouseover="if (parent.frames[3].clickenable != 0) { mb_menuover(4); popUp('elMenu4',event); } else { mb_clearmenu(); }" onclick="self.focus(); return false;" onmouseout="if (parent.frames[3].clickenable != 0) { mb_menuout(4); popDown('elMenu4'); } else { mb_clearmenu(); }" href="javascript://"><SPAN class=menu_text><NOBR menuName="elMenu4"> Gemeinsame Fkt. </NOBR></SPAN></A>
 
So for anyone reading this you may have thought I was asking a stupid question.

Believe it or not I did not think to use the DOM exactly the same way I was explaining how to do this manually.

This means that if i just try to use the DOM to click this nothing happens.
If I put my mouse over the menu then use the DOm to click this item all is good.
Thus it needs human interaction first.
So the solution is to set the HTMLElement

Then;
Code:
HTMLElement.FireEvent "onmouseover"

then if has the information for the onclick so
Code:
HTMLElement.FireEvent "onclick"

Learning alone takes a whole lot longer, but feels dam good when you solve it.
 

Users who are viewing this thread

Back
Top Bottom