Click here to Skip to main content
15,879,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i'm trying to get text from class
the element
HTML
<div id="cp-0" class="caption-line" data-time="19.5">
   <div class="caption-line-time">0:19</div>
   <div class="caption-line-text">I used to bite my tongue and hold my breath  Scared to rock the boat and make a mess</div>
</div>


What I have tried:

VB
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
Dim elems As HtmlElementCollection
                elems = WebBrowser1.Document.GetElementsByTagName("div")
                For Each elem As HtmlElement In elems
                    Dim classstr As String = elem.GetAttribute("class")
                    If ((classstr Is Nothing) And (classstr.Length <> 0)) Then
                        If classstr.ToLower().Equals("description") Then
                            Dim conStr As String = elem.GetAttribute("content")
                            PrintLine(1, conStr)
                        ElseIf classstr.ToLower().Equals("caption-line-text") Then
                            Dim conStr As String = elem.GetAttribute("content")
                            PrintLine(1, conStr)
                        End If
                    End If
                Next
                FileClose(1)
            End If
        End If
Posted
Updated 2-May-16 23:54pm
v3
Comments
MS1995 1-May-16 14:02pm    
the element
<div id="cp-0" class="caption-line" data-time="19.5">
<div class="caption-line-time">0:19</div>
<div class="caption-line-text">I used to bite my tongue and hold my breath Scared to rock the boat and make a mess</div>
</div>
Patrice T 1-May-16 14:15pm    
I did the formatting with the "code" button.
CoderzF1 1-May-16 14:20pm    
have you tried the htmlelement's innertext property? this property will return the plain text of the html element without any of the markup. in the case of formatted text, such as bold or italicized, use the innerhtml property.
MS1995 1-May-16 15:28pm    
thanks
i replaced content by innertext but it doesn't work
CoderzF1 1-May-16 16:02pm    
dont just replace the word content...it needs to read elem.innertext

You've got the initial test backwards, fix it:
VB
If ((classstr IsNot Nothing) And (classstr.Length <> 0)) Then
 
Share this answer
 
Comments
MS1995 1-May-16 15:29pm    
excuse me
but i don't know how to fix it(what do u mean?)
i removed it completely but the problem hadn't fix
what i want to get from element is
0:19
I used to bite my tongue and hold my breath Scared to rock the boat and make a mess
Matt T Heffron 1-May-16 17:38pm    
What is different about the line of code that you posted and what I posted?
You had: (classstr Is Nothing)
I had: (classstr IsNot Nothing)
You were only going to enter the Then of the If in the case of classstr being Nothing
In fact, if classstr were ever Nothing then the classstr.Length would have caused a NullReferenceException to be thrown.
So, what you have will never execute the body of the If statement!
MS1995 1-May-16 17:43pm    
o yes
thank u
but i had tried it in clwprogrammer solution but it hadn't work
Matt T Heffron 1-May-16 17:44pm    
So, after fixing this, you need to use the Visual Studio debugger and single step through the code and see where it stops doing what you expect!
Set a breakpoint on the elems = WebBrowser1... line, execute the code, and step line by line and examine the contents of the variables.
If you don't know how to use the debugger, then it's time to learn! This is a skill that is essential to your ability to write code in the future.
There are many resources on line to help with this. Google is your friend!
MS1995 1-May-16 18:36pm    
thank u
i tried breakpoints it doesn't enter this part If ((classstr Isnot Nothing) And (classstr.Length <> 0)) Then
i removed (And (classstr.Length <> 0))
then it enter the condition
but it doesn't enter any next condition
give this edit a try. if it doesnt work this time, please include the entire error shown.
i need to know exactly which onject is not set.

VB.NET
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
Dim elems As HtmlElementCollection
                elems = WebBrowser1.Document.GetElementsByTagName("div")
                For Each elem As HtmlElement In elems
                    Dim classstr As String = ""
                       classstr = elem.domelement.className '<---------------------------Changed this line
                    If classstr <> "" Then
                        If classstr.ToLower() = "description" Then
                            Dim conStr As String = elem.innertext
                            PrintLine(1, conStr)
                        ElseIf classstr.ToLower() = "caption-line-text" Then
                            Dim conStr As String = elem.Innertext
                            PrintLine(1, conStr)
                        End If
                    End If
                Next
                FileClose(1)
            End If
        End If
 
Share this answer
 
Comments
CHill60 2-May-16 14:11pm    
It is very confusing trying to determine which of the 3 solutions you have posted is meant to be the one that is solving the problem. If you need to change what you have posted then use the Improve solution link on your original post, or at the very least delete your previous efforts
CoderzF1 2-May-16 16:49pm    
ok. i removed my previous solutions. sorry about that, for some reason the improve solution or remove solution buttos werent showing up on my end. i did know about those buttons. it had me confused myself when it wasnt showing up
'thanks for all i solve it by this

If SaveFileDialog1.ShowDialog = DialogResult.OK Then
FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
For Each telement As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
Dim tElementClass As String = "caption-line-time"
Dim selementclass As String = "caption-line-text"
If telement.OuterHtml.Contains(tElementClass) AndAlso telement.OuterHtml.Contains(selementclass) Then
Dim conStr As String = telement.GetAttribute("innertext")
PrintLine(1, conStr)

End If
Next
End If


FileClose(1)

End If
 
Share this answer
 

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