Extracting Information From A <TABLE>

9/18/2007 2:09:00 AM

In InCisif.net 2.0 we added to the property HTMLControl.Length the support of the HTML control <TABLE> and <TR>.

Therefore it is easy to get the number of rows in a <TABLE> and get the number of cells in a <TR>.

The following function loops across all the rows of a <TABLE>, if a row has more than 2 cells, then we extract the text value of the cells ( <TD> ) 2 and 3.

Private Function SaveTableInfo(ByVal tableHTMLControl As HTMLControl) As Boolean

Dim r As Integer

For r = 0 To tableHTMLControl.Length - 1

If tableHTMLControl.TableRow(r).Length > 2 Then

Dim LabelP As String = tableHTMLControl.TableCell(r, 1).Text
Dim ValueP As String = tableHTMLControl.TableCell(r, 2).Text

Page.TRACE(String.Format("'[{0}]{1}'='{2}'", r, LabelP, ValueP))
End If
Next
SaveTableInfo = True
End Function

Full Source


<InCisif.net.Library.Test("MyTest's Comment", 1, TestPriority.High)> _
Public Sub USCensusBureauMassachusettsDataExtraction2()

Using t As New InCisif.net.Library.Test(Language.VBNET, Me)

Page.URL = "http://quickfacts.census.gov"
Page.WaitForPage("/index.html")
Page.WaitUntilIdle()

If Page.ControlOfType("Close this window", "htmlanchorelementclass") IsNot Nothing Then

Page.ControlOfType("Close this window", "htmlanchorelementclass").Click(True)
End If

Page.ControlOfType("Massachusetts", "htmlareaelementclass").Click()
Page.WaitForPage("/25000.html")

SaveTableInfo(Page.ControlOfType("People QuickFacts", "htmltableclass"))
SaveTableInfo(Page.ControlOfType("Business QuickFacts", "htmltableclass"))
SaveTableInfo(Page.ControlOfType("Geography QuickFacts", "htmltableclass"))

t.Passed = True
End Using
End Sub


Private
Function SaveTableInfo(ByVal tableHTMLControl As HTMLControl) As Boolean

Dim r As Integer

For r = 0 To tableHTMLControl.Length - 1

If tableHTMLControl.TableRow(r).Length > 2 Then

Dim LabelP As String = tableHTMLControl.TableCell(r, 1).Text
Dim ValueP As String = tableHTMLControl.TableCell(r, 2).Text

Page.TRACE(String.Format("'[{0}]{1}'='{2}'", r, LabelP, ValueP))
End If
Next
SaveTableInfo = True
End Function