Results 1 to 7 of 7

Thread: [RESOLVED] Need help with finding html elements

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Resolved [RESOLVED] Need help with finding html elements

    Hello,

    I have a WebBrowser, I'm tying to get text of some elements, that has inner elements. The hierarchy seems like:

    HTML Code:
    <tr class="" id="trItem33998" spry:select="selectedRow" spry:hover="hoverRow">
    
        <td style="cursor:pointer">
            <span>A4 TECH KB-2150D Q-TR MULTIMEDYA USB SİYAH SET</span> 
        </td>
        <td>
            <span>18</span>
        </td>
    
        ...
    <tr>
    There are many enumerated rows like this and I need to get all text in span tags in that table. All ids start with trItem. How can I do this using Visual Basic 2013?

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Need help with finding html elements

    Hi,

    This can be easily achieved by using the GetElementsByTagName Method of the HTMLDocument which is exposed by the WebBrowser control. You can then use various methods to iterate the returned elements to obtain your data. Have a play with this example:-

    vb.net Code:
    1. Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    2.   If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
    3.     For Each parentElement As HtmlElement In WebBrowser1.Document.GetElementsByTagName("tr").Cast(Of HtmlElement).Where(Function(x) x.GetAttribute("id").StartsWith("trItem"))
    4.       For Each spanElement As HtmlElement In parentElement.GetElementsByTagName("span")
    5.         MsgBox(spanElement.InnerText)
    6.       Next
    7.     Next
    8.   End If
    9. End Sub

    Hope that helps.

    Cheers,

    Ian

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Need help with finding html elements

    Thanks for the help. I tested and it worked fine. But now I have another problem. The web page is written in aspx and your code doesn't give me the code I see in browser panel "inspect element". I'm just able to get elemets of the source of the page. I don't know why it differs. Any ideas?

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Need help with finding html elements

    Any ideas?
    Nope and nor do I know what you mean!

    It does not matter whether it’s an ASPX page or any other type of web page it is still just basic HTML Tags. So, what do you see when you select Inspect Element on that web page?

    Ian

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Need help with finding html elements

    It shows this:

    Name:  screenshot.jpg
Views: 213
Size:  198.9 KB

    ... and they aren't shown in page source.

  6. #6
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Need help with finding html elements

    Hi,

    I think you are going to need to do some research on this subject and then think about how this affects what it is that you are trying to do. Have a start with this:-

    Is the HTML is View Source different from the HTML in Inspect Element?

    Good Luck.

    Cheers,

    Ian

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Need help with finding html elements

    ok. thanks for helping.

    Can

Tags for this Thread

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