none
HTML Agility pack Select script by its inner text RRS feed

  • Question

  • I have been trying to select a script that contains a specific text, but haven't had any luck.

    The script may have one of two texts. This is what I have tried first but it returns nothing:

    Dim SelectedNodes = Node.SelectNodes("//script[@type='text/javascript' and (contains(., 'sometext1') or contains(., 'sometext2'))]")

    I also tried:

    Dim SelectedNodes = Node.SelectNodes("//script[@type='text/javascript' and contains(., 'sometext1')]")

    Tuesday, July 9, 2019 10:49 PM

All replies

  • Seems to work in an example like this:

            Dim doc As New HtmlAgilityPack.HtmlDocument
            doc.LoadHtml("
    <html>
    <head>
    <script type='text/javascript'>
    <!--
    function f()
    {
       var t = 'sometext1';
    }
    -->
    </script>
    </head>
    <body>
    </body>")
    
            Dim Node As HtmlAgilityPack.HtmlNode = doc.DocumentNode
    
            Dim SelectedNodes = Node.SelectNodes("//script[@type='text/javascript' and contains(., 'sometext1')]")
    
    

     

    Maybe something is different in your case: Node is not an ancestor of <script>, or the type attribute is missing, etc.

     

    Wednesday, July 10, 2019 6:42 AM
  • Hi,

    add following code,the return value is what you want?

    SelectedNodes(0).InnerText

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 10, 2019 7:13 AM
    Moderator