Results 1 to 4 of 4

Thread: Multiple "TD" tagNames

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    Multiple "TD" tagNames

    Here is my code so far:
    Code:
            Dim htmlDoc As HtmlDocument = Me.WebBrowser1.Document
            Dim visibleHtmlElements As HtmlElementCollection = htmlDoc.GetElementsByTagName("TD")
            For Each str As HtmlElement In visibleHtmlElements
                If Not str.InnerText Is Nothing Then
                    Dim text As String = str.InnerText
                    If text.Contains("testWord") Then
                        MsgBox("found")
                    Else
                        MsgBox("not found")
                    End If
                End If
            Next
    There are multiple "TD" tagnames in the webpage im working with. My code looks at all of them and checks if they contain "testWord".

    What I need is some way of saying:
    If ANY text.Contains("testWord") Then
    MsgBox("found")
    Else
    MsgBox("not found")
    Or
    If THE 8th text.Contains("testWord") Then
    MsgBox("found")
    Else
    MsgBox("not found")

    Currently im getting 20 msgBox's telling me "found" and "not found". I just want one.

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Multiple "TD" tagNames

    Then exit the loop as soon as you fine the first one.

    Code:
    Dim htmlDoc As HtmlDocument = Me.WebBrowser1.Document
            Dim visibleHtmlElements As HtmlElementCollection = htmlDoc.GetElementsByTagName("TD")
            For Each str As HtmlElement In visibleHtmlElements
                If Not str.InnerText Is Nothing Then
                    Dim text As String = str.InnerText
                    If text.Contains("testWord") Then
                        MsgBox("found")
                        Exit For
                    Else
                        MsgBox("not found")
                        Exit For
                    End If
                End If
            Next
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    PA
    Posts
    365

    Re: Multiple "TD" tagNames

    If you're really finding 20 of them, are they all returning the same? You mention any or the 8th innertext. Will the first one found suffice, if so look above, otherwise, just keep a count for each one found, then when the count is 8, after it finds that msgbox you can exit the for loop.

  4. #4
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    Re: Multiple "TD" tagNames

    Since you're in a VB.NET forum, I will also suggest that you use MessageBox.Show() instead of MsgBox().
    Strange how many people still even know of MsgBox().

    Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7

    SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
    [Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]

    [.NET and MySQL Quick Guide]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width