Home > Archive > Visual Basic > July 2006 > Trying to get elements from a web page via VB
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Trying to get elements from a web page via VB
|
|
| Newbie@gmail.com 2006-06-17, 7:59 am |
| I am attempting to write a little utility to check my mail at Yahoo.
I found an older posting that shows how to get the elements from a web
page via VB using the browser control.
When I tried to use this code, I have problems.
WB.Document.All(i).Type generates this error
"Object doesn't support this action"
and
WB.Document.All(i).Value generates the same error.
However, WB.Document.All(i).TagName and
WB.Document.All(i).OuterText work fine.
WB is the WebBrowser on the form.
Please tell me what I am doing wrong. When I typed the "." after WB,
I got the list of all properties, but when I put the "." after
document, I did not see the properties of the document so I typed
All(i) and then ran it to no avail.
Private Sub cmdCheckMail_Click()
Dim i As Integer
WB.Navigate txtURL.Text
Do
DoEvents
If WB.Busy = False Then Exit Do
Loop
On Error Resume Next
For i = 0 To WB.Document.All.Length - 1
txtNotes.Text = txtNotes.Text & WB.Document.All(i).TagName & vbCrLf
txtNotes.Text = txtNotes.Text & WB.Document.All(i).OuterText & vbCrLf
txtNotes.Text = txtNotes.Text & WB.Document.All(i).Type & vbCrLf
txtNotes.Text = txtNotes.Text & WB.Document.All(i).Value & vbCrLf
Next
End Sub
Private Sub Form_Load()
txtURL.Text = "https://login.yahoo.com/config/mail?.intl=us"
End Sub
I have copied the old posting below.
Thans for any help you can give.
>WebBrowser1.Document.All will hold every element in a HTMP document.
>You will wish to make a note of which element(s) are the form's text
>boxes, and which element(s) are the form's buttons. Since a form
>generally has only one "submit" button, but may have many text boxes,
>you will need to write dowen (perhaps on a piece of paper) which
>elements you are working with.
>
>To list the elements, first navigate to the web site (via URL) in
>question and then run a script to list the elements.
>
>Private Sub Command1_Click()
> Dim i As Integer
>
> Open "C:\temp\Element-list.txt" For Output As #1
>
> WebBrowser1.Navigate "http://holysmoke.org/"
> Do
> DoEvents
> If WebBrowser1.Busy = False Then Exit Do
> Loop
>
> On Error Resume Next
>
> For i = 0 To WebBrowser1.Document.All.length - 1
> Debug.Print i, "Tag Name: ";
>WebBrowser1.Document.All(i).TagName
> Debug.Print i, "Text: "; WebBrowser1.Document.All(i).OuterText
> Debug.Print i, "Type: "; WebBrowser1.Document.All(i).Type
> Debug.Print i, "Value: "; WebBrowser1.Document.All(i).Value
> Debug.Print
>
> Print #1, i, "Tag Name: "; WebBrowser1.Document.All(i).TagName
> Print #1, i, "Text: "; WebBrowser1.Document.All(i).OuterText
> Print #1, i, "Type: "; WebBrowser1.Document.All(i).Type
> Print #1, i, "Value: "; WebBrowser1.Document.All(i).Value
> Print #1, ""
> Next
> Close
>End Sub
>
>For the text boxes, you are looking for the elements called "OPTION".
>You will wish to fill those elements with the Value property
>
> WebBrowser1.Document.All(-the-number-).Value = "Hello World"
>
>For the submit button, you probably want the last "INPUT" element,
>however if the HTML form has buttons after the button you want, you
>will have to determine the actual element number. Once you find that
>element number, you may use the .Click method to "push" that button.
>
> WebBrowser1.Document.All(-the-number-).Click
>
>In the example above, the holysmoke.org web page has HTML form with a
>text box at element 121. The submit button is element 122. If I wanted
>to fill out that text box I would add the line to my code:
>
> WebBrowser1.Document.All(121).Value = "Bob Minton"
>
>Then to submit that form, I would add:
>
> WebBrowser1.Document.All(122).Click
> Do
> DoEvents
> If WebBrowser1.Busy = False Then Exit Do
> Loop
| |
| desertphile@hotmail.com 2006-07-03, 6:56 pm |
| Newbie@gmail.com wrote:
> I am attempting to write a little utility to check my mail at Yahoo.
>
> I found an older posting that shows how to get the elements from a web
> page via VB using the browser control.
>
> When I tried to use this code, I have problems.
>
> WB.Document.All(i).Type generates this error
> "Object doesn't support this action"
>
> and
>
> WB.Document.All(i).Value generates the same error.
>
> However, WB.Document.All(i).TagName and
> WB.Document.All(i).OuterText work fine.
>
> WB is the WebBrowser on the form.
Greetings.
Not all elements in the .All collection will have the same, nor all,
tags. This is because HTML elements are different: text box elements on
an HTML form will have a .Value tag, but paragraph elements <p> will
not, for example. All, or almost all, HTML elements will have the
..Outertext, .InnerText, .OuterHTML, .InnerHTML, and several other tags.
The only way I have found that works is to trap the errors via "On
Error Resume Next" when looping through the elements of a Document
collection.
> Please tell me what I am doing wrong. When I typed the "." after WB,
> I got the list of all properties, but when I put the "." after
> document, I did not see the properties of the document so I typed
> All(i) and then ran it to no avail.
That is the same behavior on my computer. The document.all() collection
does not give you properties (and methods) because it does not know
them at design time. You can, however, make your own collections using
the .Tags("") function. For example, the following code grabs all text
within the <TD> tags for the second table on a web page:
Dim i As Long, oLinks As HTMLLIElement, Tables As
IHTMLElementCollection
Dim j As Long, tb As HTMLTable
Dim td As IHTMLElementCollection
Set Tables = WebBrowser1.Document.All.tags("TABLE")
If Not IsNull(Tables) Then
Set tb = Tables.Item(2)
Set td = tb.Document.All.tags("TD")
For j = 0 To td.length
Debug.Print j;Trim(td.Item(j).outerText)
Next
End If
Set td = Nothing
Set tb = Nothing
Set Tables = Nothing
> Private Sub cmdCheckMail_Click()
> Dim i As Integer
>
> WB.Navigate txtURL.Text
>
> Do
> DoEvents
> If WB.Busy = False Then Exit Do
> Loop
>
> On Error Resume Next
>
> For i = 0 To WB.Document.All.Length - 1
> txtNotes.Text = txtNotes.Text & WB.Document.All(i).TagName & vbCrLf
>
> txtNotes.Text = txtNotes.Text & WB.Document.All(i).OuterText & vbCrLf
>
> txtNotes.Text = txtNotes.Text & WB.Document.All(i).Type & vbCrLf
>
> txtNotes.Text = txtNotes.Text & WB.Document.All(i).Value & vbCrLf
> Next
>
> End Sub
>
> Private Sub Form_Load()
> txtURL.Text = "https://login.yahoo.com/config/mail?.intl=us"
> End Sub
>
> I have copied the old posting below.
>
> Thans for any help you can give.
[color=darkred]
| |
| Gert Wietzorek 2006-07-03, 6:56 pm |
| Hi,
Newbie@gmail.com schrieb:
>
> WB.Document.All(i).Type generates this error
> "Object doesn't support this action"
>
> and
>
> WB.Document.All(i).Value generates the same error.
>
> However, WB.Document.All(i).TagName and
> WB.Document.All(i).OuterText work fine.
>
> WB is the WebBrowser on the form.
>
> Please tell me what I am doing wrong. When I typed the "." after WB,
> I got the list of all properties, but when I put the "." after
> document, I did not see the properties of the document so I typed
> All(i) and then ran it to no avail.
Set a reference to the "Microsoft HTML Object Library" and declare an
object of type HTMLDocument
dim doc as HTMLDocument
set doc=web.document
then you get the properties when typing "."
good luck
Gert
*** answer only to the newsgroup, e-mail adresses are not valid ***
|
|
|
|
|