以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  狐表如何操作网页中里面的iframe的document  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=72001)

--  作者:Fotable
--  发布时间:2015/7/22 0:11:00
--  狐表如何操作网页中里面的iframe的document

 

 

我们知道document对象是window对象的一个子对象,所以我们可以通过 document.getElementById(“iframe ID”).contentWindow.document来获取iframe的 document对象,相当于contentDocument属性。

 

用vb测试

Dim doc As HTMLDocument
Set doc = WebBrowser1.document
Dim t As IHTMLElement
Dim a As HTMLDocument
Set a = doc.getElementById("ueditor_0").contentWindow.document

这些语句是没有问题的

 

但在狐表中使用这个方法

\'Dim htmFrame As System.Windows.Forms.Htmldocument= web.Document.GetElementById("ueditor_0").contentwindow.document    \'ueditor_0  指向一个iframe

 

未找到类型“HtmlElement”的公共成员“contentWindow”。


--  作者:Fotable
--  发布时间:2015/7/22 0:26:00
--  
 

Public lb As String \'定义一个全局变量作为标志,根据这个标志执行对应操作

Public Isgo As Boolean \'定义一个全局变量逻辑值,根据这个逻辑值判断是否继续进行运作

\'定义一个WebBrowser加载完成事件,(每当浏览器加载完成当前页面就会执行该事件)

Public Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

If Isgo Then \'判断一下ISGO是否需要执行操作 如果是TRUE说明可以继续执行操作, 如果是FALSE说明已经执行完毕,或者点击了停止.

    Dim web As System.Windows.Forms.WebBrowser = sender \'把触发该事件的Sender转换为WebBrowser

    If web.ReadyState=System.Windows.Forms.WebBrowserReadyState.Complete\'由于加载完成事件会触发多次,分别为页面数据加载完成,DOM树加载完成.所以这里要判断一下.确保所有东西都加载完成再执行下面的操作

        Select Case lb \'分别根据LB标志的值来执行不同的操作

            Case "访问"

                If web.Document.Body.OuterHtml.Contains("javascript:Dv_Win_Modal.Open(\'login_win.asp?winaction=winlogin\',500,350);") Then \'判断一下当前页面的是否包含这串字符串(用来判断是否已经处于登录状态)

                    Dim hec As System.Windows.Forms.HtmlElementCollection = web.Document.GetElementsByTagName("a") \'获得当前页面的A标签HTML元素集合

                    For i As Integer = 0 To hec.Count-1 \'循环所有A标签

                        If hec(i).OuterHtml.Contains("javascript:Dv_Win_Modal.Open(\'login_win.asp?winaction=winlogin\',500,350);") Then \'判断该A标签是否包含这个字符串(如果包含说明该A标签是登录按钮)

                            hec(i).InvokeMember("click") \'执行该标签的点击方法,从而实现点击登录按钮

                            lb="登录"  \'把LB设置为"登录" 这样下一步就回执行Case "登录" 里面的代码

                            Exit For  \'既然已经找到了这个A标签,没有必须继续循环了.退出循环

                        End If

                    Next

                Else \'发现页面没有包含该字符串,说明没有登录按钮,处于已经登录状态

                    Dim hec As System.Windows.Forms.HtmlElementCollection = web.Document.GetElementsByTagName("a")  \'获得当前页面的A标签HTML元素集合

                    For i As Integer = 0 To hec.Count-1 \'循环所有A标签

                        If hec(i).OuterHtml.Contains("logout.asp") Then  \'判断这个A标签是否包含这个字符串,如果包含说明是退出登录按钮

                            hec(i).InvokeMember("click") \'执行该A标签的点击方法,从而实现点击退出按钮.

                            lb="访问" \'\'把LB设置为"登录" 这样下一步就回执行Case "访问" 里面的代码

                            Exit For

                        End If

                    Next

                End If

            Case "登录"

                web.Document.Window.Frames(1).Document.GetElementById("username").SetAttribute("Value","test138") \'获得第二个FrAMES框架里面的ID为"username"的的HTML标签元素,并且将其Value设置为"test138"

                web.Document.Window.Frames(1).Document.GetElementById("pwd1").SetAttribute("Value","a110110") \'获得第二个FrAMES框架里面的ID为"pwd1"的的HTML标签元素,并且将其Value设置为"a110110"

                \'如果元素不是在Frame框架中,那么直接 web.Document.GetElementById 即可

                \'如果元素没有ID怎么办? 按照上面的方法,获得指定类型的元素集合,然后循环判断是否包含该字段的方法来获得.

                \'下面的代码都大同小异,就不一一介绍了.

                Dim hec As System.Windows.Forms.HtmlElementCollection = web.Document.Window.Frames(1).Document.GetElementsByTagName("input")

                For i As Integer = 0 To hec.Count-1

                    If hec(i).OuterHtml.Contains("立即登录") Then

                        hec(i).InvokeMember("click")

                        lb="去精华板块"

                        Exit For

                    End If

                Next

            Case "去精华板块"

                web.Navigate("http://www.foxtable.com/bbs/index.asp?boardid=2&TopicMode=0&List_Type=6,0,0&page=")

                lb="查找帖子"

            Case "查找帖子"

                Dim hec As System.Windows.Forms.HtmlElementCollection = web.Document.GetElementsByTagName("a")

                For i As Integer = 0 To hec.Count-1

                    If hec(i).OuterHtml.Contains("不怕空袭的通用多选筛选树") Then

                        hec(i).InvokeMember("click")

                        lb="下载文件"

                        Exit For

                    End If

                Next

            Case "下载文件"

                Dim hec As System.Windows.Forms.HtmlElementCollection = web.Document.GetElementsByTagName("a")

                For i As Integer = 0 To hec.Count-1

                    If hec(i).OuterHtml.Contains("点击浏览该文件:多选筛选树.table") Then

                        hec(i).InvokeMember("click")

                        lb="下载文件"

                        Exit For

                    End If

                Next

                Isgo=False

        End Select

    End If

End If

End Sub


--  作者:大红袍
--  发布时间:2015/7/22 9:20:00
--  

Dim web As System.Windows.Forms.WebBrowser = e.Form.Controls("WebBrowser1").BaseControl
Dim data As String =  web.Document.Window.Frames(1).Document.Body.InnerHtml