locked
WebBrowser + IFrame

    Question

  • Hello~
    How can I access the HTML source of IFrame within a page by using WebBrowser control?
    Thanks~
    Tuesday, December 18, 2007 3:46 PM

Answers

  • Hi Albert,

     

    Presume that a web page C:\1.htm contains an IFrame named Searchbar,

    Code Block

    <html>

    <body>

    Webpage containing a IFrame.

    <br>

    <Iframe id="Searchbar" name="Searchbar" src="Search.htm">

    </< SPAN>body>

    </< SPAN>html>

    and the fraSearchbar IFrame contains a TextBox named txtSearchFor and a Button named btnGo.

    Code Block

    <html>

    <body>

    This is an IFrame.

    <form>

    <input name="txtSearchFor" type="text" id="txtSearchFor" maxlength="500"/>

    <input name="btnGo" type="button" id="btnGo" title="Go" onclick="Search()" value=" GO " />

    </< SPAN><form>

    </< SPAN><body>

    </< SPAN><html>

     

    Then you can locate and access these elements inside the Searchbar iFrame like this:

    Code Block

    Public Class Form1

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            WebBrowser1.Dock = DockStyle.Fill

            WebBrowser1.Navigate("C:\1.htm")

        End Sub

     

        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

     

            Dim CurrentWindow As HtmlWindow = WebBrowser1.Document.Window

            For Each Frame As HtmlWindow In CurrentWindow.Frames 'Get all frames

                If Frame.Name = "Searchbar" Then

     

                    Dim btnElementCollection As HtmlElementCollection = Frame.Document.GetElementsByTagName("Input")

                    For Each curElement As HtmlElement In btnElementCollection

                        Dim controlName As String = curElement.GetAttribute("Name").ToString

     

                        If controlName = "txtSearchFor" Then

                            curElement.SetAttribute("Value", "Search fior text")

                        ElseIf controlName = "btnGo" Then

                            curElement.InvokeMember("Click")

                        End If

                    Next

     

                End If

            Next

     

        End Sub

     

    End Class

     

    Similar issue: How to read HTML Content inside the Frames?

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2546517&SiteID=1

     

    Regards,

    Martin

    Wednesday, December 19, 2007 5:55 AM

All replies

  • Hi Albert,

     

    Presume that a web page C:\1.htm contains an IFrame named Searchbar,

    Code Block

    <html>

    <body>

    Webpage containing a IFrame.

    <br>

    <Iframe id="Searchbar" name="Searchbar" src="Search.htm">

    </< SPAN>body>

    </< SPAN>html>

    and the fraSearchbar IFrame contains a TextBox named txtSearchFor and a Button named btnGo.

    Code Block

    <html>

    <body>

    This is an IFrame.

    <form>

    <input name="txtSearchFor" type="text" id="txtSearchFor" maxlength="500"/>

    <input name="btnGo" type="button" id="btnGo" title="Go" onclick="Search()" value=" GO " />

    </< SPAN><form>

    </< SPAN><body>

    </< SPAN><html>

     

    Then you can locate and access these elements inside the Searchbar iFrame like this:

    Code Block

    Public Class Form1

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            WebBrowser1.Dock = DockStyle.Fill

            WebBrowser1.Navigate("C:\1.htm")

        End Sub

     

        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

     

            Dim CurrentWindow As HtmlWindow = WebBrowser1.Document.Window

            For Each Frame As HtmlWindow In CurrentWindow.Frames 'Get all frames

                If Frame.Name = "Searchbar" Then

     

                    Dim btnElementCollection As HtmlElementCollection = Frame.Document.GetElementsByTagName("Input")

                    For Each curElement As HtmlElement In btnElementCollection

                        Dim controlName As String = curElement.GetAttribute("Name").ToString

     

                        If controlName = "txtSearchFor" Then

                            curElement.SetAttribute("Value", "Search fior text")

                        ElseIf controlName = "btnGo" Then

                            curElement.InvokeMember("Click")

                        End If

                    Next

     

                End If

            Next

     

        End Sub

     

    End Class

     

    Similar issue: How to read HTML Content inside the Frames?

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2546517&SiteID=1

     

    Regards,

    Martin

    Wednesday, December 19, 2007 5:55 AM
  • Thank you for your great solution!


    But I am facing another problem.
    If A.html is hosting at www.abc.com contains B.html in an iframe. But B.html is hosting at www.xyz.com.
    The above coding would return exception like below...

    "{"Access Denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"}".

    Any suggestions? Thanks a lot!
    Wednesday, December 19, 2007 4:21 PM
  • Hi Albert,

     

    Based on my understanding, http://www.abc.com webpage contains an IFrame and the IFrame references to http://www.xyz.com webpage. Right?

    Code Block

    ' http://www.abc.com webpage

     

    <Iframe id="Searchbar" name="Searchbar" src="http://www.xyz.com">

     

    Can you provide the two websites (www.abc.com and www.xyz.com) in order to let me repro the issue?

     

    "{"Access Denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"}"

    -> The exception indicates you have no permission to access certain webpage (e.g. http://www.xyz.com), maybe firstly you need to login in with username and password.

    Friday, December 28, 2007 7:10 AM
  • Hi Albert,

     

    Presume that a web page C:\1.htm contains an IFrame named Searchbar,

    Code Block

    <html>

    <body>

    Webpage containing a IFrame.

    <br>

    <Iframe id="Searchbar" name="Searchbar" src="Search.htm">

    </< SPAN>body>

    </< SPAN>html>

    Then you can locate and access these elements inside the Searchbar iFrame like this:

    Similar issue: How to read HTML Content inside the Frames?

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2546517&SiteID=1

     

    Regards,

    Martin

    Code Block

    Public Class Form1

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            WebBrowser1.Dock = DockStyle.Fill

            WebBrowser1.Navigate("C:\1.htm")

        End Sub

     

        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

     

            Dim CurrentWindow As HtmlWindow = WebBrowser1.Document.Window

            For Each Frame As HtmlWindow In CurrentWindow.Frames 'Get all frames

                If Frame.Name = "Searchbar" Then

     

                    Dim btnElementCollection As HtmlElementCollection = Frame.Document.GetElementsByTagName("Input")

                    For Each curElement As HtmlElement In btnElementCollection

                        Dim controlName As String = curElement.GetAttribute("Name").ToString

     

                        If controlName = "txtSearchFor" Then

                            curElement.SetAttribute("Value", "Search fior text")

                        ElseIf controlName = "btnGo" Then

                            curElement.InvokeMember("Click")

                        End If

                    Next

     

                End If

            Next

     

        End Sub

     

    End Class

     

    and the fraSearchbar IFrame contains a TextBox named txtSearchFor and a Button named btnGo.

    Code Block

    <html>

    <body>

    This is an IFrame.

    <form>

    <input name="txtSearchFor" type="text" id="txtSearchFor" maxlength="500"/>

    <input name="btnGo" type="button" id="btnGo" title="Go" onclick="Search()" value=" GO " />

    </< SPAN><form>

    </< SPAN><body>

    </< SPAN><html>

     




    Hi Martin!,

    I was having a similar issue with iframes and the webbrowser control. I tried to impliment the above codes into my situation and it still seems to not work. Please take a look at the following thread to see if you can help me out on this:

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/64a29566-9123-4f7c-a862-b2dfd2f65094

    I remember that I contacted you awhile back about iframes and you said that you were going to continue investigating the solution to this issue. Well, I was just wondering if you ever figured it out how to get the webbrowser control to work well with iframes.

    I would appreciate your help.
    Tuesday, March 09, 2010 11:54 PM