Getting elements within an element...

B

BobRoyAce

I've got a WebBrowser control on a form, called WebBrowser1, and,
through code, I navigate to a web page. Now, on this web page, among
many other things, there is a <TABLE > with several rows, each having
five columns, two of which I care about.

Well I can use code as follows to find a particular TABLE on the
page...

Dim oElement As HtmlElement = WebBrowser1.Document.GetElementById
("tblSubmitSales")

This works great as I get back the TABLE element I was after.

Now, what I need to do is go after each ROW element in the returned
table element. Then, I'll want to be able to interrogate each Cell
(<TD>) element within each ROW element.

Is there a straightforward way to do this. Ideally, there'd be some
method, similar to "GetElementsByTagName", as then I could do
something like...

Dim oRowCollection As HtmlElementCollection
oRowCollection = oElement.GetElementsByTagName("TR")

Dim oCellCollection as HtmlElementCollection
For Each oElement as HtmlElement in oRowCollection
oCellCollection = oElement.GetElementsByTagName("TD")

' Do something with the cell elements here...
Next

I'd appreciate any insights on this.

In addition, are there any really good sources of detailed/extensive
information on using the WebBrowser control?
 
L

Lloyd Sheen

BobRoyAce said:
I've got a WebBrowser control on a form, called WebBrowser1, and,
through code, I navigate to a web page. Now, on this web page, among
many other things, there is a <TABLE > with several rows, each having
five columns, two of which I care about.

Well I can use code as follows to find a particular TABLE on the
page...

Dim oElement As HtmlElement = WebBrowser1.Document.GetElementById
("tblSubmitSales")

This works great as I get back the TABLE element I was after.

Now, what I need to do is go after each ROW element in the returned
table element. Then, I'll want to be able to interrogate each Cell
(<TD>) element within each ROW element.

Is there a straightforward way to do this. Ideally, there'd be some
method, similar to "GetElementsByTagName", as then I could do
something like...

Dim oRowCollection As HtmlElementCollection
oRowCollection = oElement.GetElementsByTagName("TR")

Dim oCellCollection as HtmlElementCollection
For Each oElement as HtmlElement in oRowCollection
oCellCollection = oElement.GetElementsByTagName("TD")

' Do something with the cell elements here...
Next

I'd appreciate any insights on this.

In addition, are there any really good sources of detailed/extensive
information on using the WebBrowser control?


There is a method GetElementsByTagName. If you have the table element you
can do exactly what you are describing.

An example which I use:

Dim doc As HtmlDocument = WebBrowser1.Document
Dim tab As HtmlElement = doc.GetElementById("ExpansionTable1")
ListView1.Items.Clear()
Dim trs As HtmlElementCollection = tab.GetElementsByTagName("TR")
For Each tr As HtmlElement In trs
Dim tmp As Integer = 1
If tr.OuterHtml.Contains("class=visible") And
tr.OuterHtml.Contains("id=trlink") Then
Dim tds As HtmlElementCollection = tr.GetElementsByTagName("TD")
Dim li As New ListViewItem
li.Text = tds(4).InnerText
li.SubItems.Add(tds(2).InnerText)
ListView1.Items.Add(li)
End If
Next

LS
 
B

BobRoyAce

There is a method GetElementsByTagName.  If you have the table element you
can do exactly what you are describing.

Well, I'll be...I never even thought to check that...kinda silly of
me. :)

Thanks for the insights, and the sample code. I was able to adapt
perfectly for my needs!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top