Click here to Skip to main content
15,510,484 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all!

Im trying to get element from htm file to vb.net but when I excute this command, i see a problem with textbox2.text = arrowtag(2) <- for that value.

Problem is:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


& when I change command see different problem: Index was outside the bounds of the array.


Dim ele As HtmlElement = WebBrowser1.Document.GetElementsByClassName("invoice-status text-success alert alert-success")(2)
TextBox2.Text = ele.InnerText


Please help me ! Thank you very much :)
Hope to find a solution

What I have tried:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    WebBrowser1.Navigate(TextBox1.Text)

    Dim ArrayTag As New ArrayList
    For Each item As HtmlElement In WebBrowser1.Document.GetElementsByTagName("span")
        ArrayTag.Add(item.InnerText)
    Next
    TextBox2.Text = ArrayTag(2)
End Sub


......................................................

Module Module1
<runtime.compilerservices.extension()> _
Public Function GetElementsByClassName(ByVal Source As HtmlDocument, ByVal ClassName As String) As HtmlElement()
Dim output As New List(Of HtmlElement)
For i As Integer = 0 To Source.All.Count - 1
Try
If (Source.All(i).GetAttribute("className") = ClassName) Then
output.Add(Source.All(i))
End If
Catch ex As Exception
End Try
Next
Return output.ToArray()
End Function
End Module
Posted
Updated 19-Jun-21 7:32am
v4
Comments
Patrice T 19-Jun-21 11:49am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
ionMEMBER 19-Jun-21 12:05pm    
Thank you!
Richard MacCutchan 19-Jun-21 11:03am    
ArrayTag contains less than three items; never assume you will get the number of items you want. Use the debugger to find out why it does not contain enough.
ionMEMBER 19-Jun-21 12:15pm    
ArrayTag contains more than three items, whatever number i decide to arragtag, same problem. Maybe is all about the module
Richard MacCutchan 19-Jun-21 12:50pm    
Have you confirmed that with the debugger?

1 solution

Quote:
ArrayTag contains more than three items, whatever number i decide to arragtag, same problem. Maybe is all about the module

Stop guessing, and make sure with the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
ionMEMBER 19-Jun-21 13:16pm    
This command is connected to the module (getelementsbyclassname).

Dim ele As HtmlElement = WebBrowser1.Document.GetElementsByClassName("invoice-status text-success alert alert-success")(2)
TextBox2.Text = ele.InnerText


Breakpoint: For i As Integer = 0 To Source.All.Count - 1
ionMEMBER 19-Jun-21 13:18pm    
And this command is not with the module. No breakpoint but still with that problem:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Dim ArrayTag As New ArrayList
For Each item As HtmlElement In WebBrowser1.Document.GetElementsByTagName("span")
ArrayTag.Add(item.InnerText)
Next
TextBox2.Text = ArrayTag(2)
Ralf Meier 20-Jun-21 3:36am    
It is like Patrice allready has mentioned : you should set a Breakpoint (perhaps at the line 'TextBox2.Text = ArrayTag(2)') and look with the Debugger what REALLY is inside ArrayTag - I suppose that you will see that there are only 2 Elements inside ArrayTag (Index 0 and 1). Give it a try ... ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900