Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am attempting to get text from a Element on a website

Websites Element Code:

HTML
<div class="info-box">
<span class="info-box-icon bg-yellow">

</span>
<div class="info-box-content">
<span class="info-box-text">TOTAL MEMBERS</span>
<span class="info-box-number">34,396</span>
</div>
</div>


I am attempting to get 34,396 from info-box-number.

There are 4 of these boxes that I need to get them from. They are all the same except the Info-Box-Text and Info-Box-Number are different.

What I have tried:

I attempted to use GetAttribute but, I kept getting a error.

VB
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   Textbox1.Text = WebBrowser1.Document.GetElementsByTagName("info-text-box").GetAttribute("info-text-number").ToString()
End Sub


After I get the number I want it to go into a TextBox.

-UPDATE-
Attempted
VB
TextBox1.Text = WebBrowser1.Document.GetElementById("info-box-number").InnerText 


Error I got:
An unhandled exception of type 'System.NullReferenceException' occurred in MyTool.exe

Additional information: Object reference not set to an instance of an object.
Posted
Updated 20-Jun-16 4:16am
v2
Comments
[no name] 19-Jun-16 19:37pm    
Looks pretty much like the Document was not loaded yet at the time when you tried to process the elements. First make sure that the Document is loaded, try on a MessageBox to display all the content of the document in the Load() Event. If you get an error there you know it was not ready.I believe there is an Event also that notifies when the WebBrowser has downloaded the document, but im not sure.
Member 12592475 19-Jun-16 22:36pm    
I attempted that and I made it wait 5 seconds and then I got this error

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an invocation.

1 solution

Use the
XML
Event DocumentCompleted()
of the WebBrowser, instead of the Form Load() Event.

VB
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        
  TextBox1.Text = WebBrowser1.Document.GetElementById("infobox-number").InnerText
  MsgBox("DOCUMENT_COMPLETED")
End Sub


... but i noticed that the DocumentCompleted() Event raised dozens of times, also when i scrolled up&down.
Here i give some code that i used years ago on my project. It does not use the WebBrowser, instead it downloads the data directly. Try to tweak it for your needs.

VB
Private Function GetInfo() As String
dim body as string = ""
        Try 
            Dim ur As New Uri("http://www.find-ip-address.org/")
            Dim req As HttpWebRequest = CType(HttpWebRequest.Create(ur), HttpWebRequest)
            Dim res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)
            If (res.StatusCode = HttpStatusCode.OK) Then
                Dim receiveStream As Stream = res.GetResponseStream()
                Dim readStream As StreamReader = Nothing
                If (res.CharacterSet = Nothing) Then
                    readStream = New StreamReader(receiveStream)
                Else
                    readStream = New StreamReader(receiveStream, Encoding.GetEncoding(res.CharacterSet))
                    Dim data As String = readStream.ReadToEnd()
                    'data contains all HTML text
'################################
                    res.Close()
                    readStream.Close()

                    Dim left = "My IP Country Name:"
                    Dim right = "IP Address Lookup Location"
                    Dim indexLeft As Integer = data.IndexOf(left)
                    Dim indexRight As Integer = data.IndexOf(right)
                    body = data.Substring(indexLeft + left.Length, indexRight - indexLeft - left.Length)
                End If
            End If
        End Try
            Return body
End Function
 
Share this answer
 
Comments
Vijay Manglani 7-Apr-18 3:41am    
what is "body" in the last line before the End If's?

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