51Testing软件测试论坛

标题: 学习VB.net对IE的控制一(希望对大家有点帮助) [打印本页]

作者: lonetan    时间: 2008-2-29 02:25
标题: 学习VB.net对IE的控制一(希望对大家有点帮助)
'首先在工程中加入对
    '"Microsoft Internet Controls" (Shdocvw.dll) 和 "Microsoft HTML Object Library" (Mshtml.dll)的引用

    '指定浏览器对象的Document
    Public mDocument As Object
    '参数为网页标题
    Public Sub mComGetIEWindows(ByVal IETitle As String)
        '浏览器对象集合(包含IE也包含资源管理器)
        Dim SWs As New SHDocVw.ShellWindows
        Dim IE As SHDocVw.InternetExplorer
        Dim Doc
        '循环变量
        For Each IE In SWs


            Doc = IE.Document
            If TypeOf Doc Is mshtml.HTMLDocument Then
                'if this is an HTML page, display the title
                'may or may not be the same as LocationName
                If Doc.title = IETitle Then
                    mDocument = Doc
                    Exit Sub
                End If
            End If
        Next

    End Sub


    '首先请打开以下地址:
    'http://bbs.163.com
    '观看页面,有一个输入框里面有字符,我们的目的就是获取其内容
    '查看网页源文件,注意以下这行,其中name="so_title"是我们引用该文本框要用的
    '<input name="so_title" type="text" value="输入文章标题" size=" " class="s2">




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '获取标题为网易论坛的IE对象
        mComGetIEWindows("网易论坛")
        '如果存在该窗口,那么显示其中name属性为so_title的页面对象的内容
        If mDocument Is Nothing Then
            MsgBox("unkonwn")
        Else
            MsgBox(mDocument.body.All("so_title").Value)
        End If

    End Sub
作者: lonetan    时间: 2008-2-29 02:33
MSDN上的例子
In order to run the following code, it is necessary to add a reference to "Microsoft Internet Controls" (Shdocvw.dll) and "Microsoft HTML Object Library" (Mshtml.dll) to the Visual Basic project:

Dim SWs As New SHDocVw.ShellWindows
Dim IE As SHDocVw.InternetExplorer

Private Sub Form_Load()
   Dim Doc
   List1.Clear
   List2.Clear

   Text1.Text = SWs.count

   For Each IE In SWs
      List1.AddItem IE.LocationName

      Set Doc = IE.Document
      If TypeOf Doc Is HTMLDocument Then 'HTMLDocument 貌似应该为mshtml.HTMLDocument
         'if this is an HTML page, display the title
         'may or may not be the same as LocationName
         List2.AddItem Doc.Title
      End If
   Next
End Sub
作者: walker1020    时间: 2008-2-29 20:36
非常感谢楼主的共享!
作者: sm_zx    时间: 2008-3-1 16:44
又学到了点东东 谢谢分享!
作者: lonetan    时间: 2008-3-8 14:33
Dim fDoc As mshtml.HTMLDocument
        Dim fra As mshtml.HTMLFrameElement
        Dim frawnd As mshtml.HTMLWindow2
        Dim x As Integer
        Dim wb As SHDocVw.WebBrowser

        wb = New SHDocVw.WebBrowser
        'wb.Navigate("E:\longtan\workspace\web\qtp\frame.html")
        
        fDoc = AxWebBrowser1.Document
        '   Loop through the frames.
        'MsgBox(fDoc.frames.length)
        Call EnumerateFrames(fDoc, list)
        list.Items.Add(AxWebBrowser1.Document.frames(1).document.title)
        list.Items.Add(AxWebBrowser1.Document.frames(1).document.getElementById("username").value)
        AxWebBrowser1.Document.frames(1).document.getElementById("username").value = "haha"

        fDoc = Nothing
        frawnd = Nothing
        fra = Nothing

Private Sub EnumerateFrames(ByVal doc As mshtml.HTMLDocument, ByVal list As ListBox)
        On Error Resume Next
        Dim i As Integer
        Dim frawnd As mshtml.HTMLWindow2
        Dim fra As mshtml.HTMLFrameElement
        'documentelement和contentwindow
        MsgBox(doc.frames.length)
        For i = 0 To doc.frames.length - 1
            frawnd = doc.frames.item(i)
            fra = frawnd.frameElement
            MsgBox(fra.src)
            list.Items.Add(fra.src)
            list.Items.Add(fra.document.document.title)

        Next
    End Sub

[ 本帖最后由 lonetan 于 2008-3-8 15:37 编辑 ]
作者: lonetan    时间: 2008-3-8 14:35
标题: 用InternetExplorer.Application来操作,获得对象的时候咋不对?
Dim IE As SHDocVw.InternetExplorer
        Dim fDoc As mshtml.HTMLDocument
        Dim frms As mshtml.HTMLFrameElement
        Dim frawnd As mshtml.HTMLWindow2
        Dim fra As mshtml.HTMLFrameElement
        Dim x

        IE = CreateObject("InternetExplorer.Application")
        IE.Visible = True
        IE.Navigate("E:\longtan\workspace\web\qtp\frame.html")
        fDoc = IE.Document

        MsgBox(fDoc.frames.length)
        If fDoc.frames.length <> 0 Then
            For x = 0 To fDoc.frames.length - 1

                frawnd = fDoc.frames.item(x)
                fra = frawnd.frameElement
                MsgBox(fra.src)

            Next
        End If
作者: lantianwei    时间: 2008-3-8 14:56
原帖由 lonetan 于 2008-3-8 14:33 发表
Dim fDoc As mshtml.HTMLDocument
        Dim fra As mshtml.HTMLFrameElement
        Dim frawnd As mshtml.HTMLWindow2
        Dim x As Integer
        Dim wb As SHDocVw.WebBrowser

        wb = Ne ...

不错,支持一下!@
作者: 小孩    时间: 2008-3-8 16:08
学习学习
作者: rita.LQ    时间: 2008-7-24 11:11
标题: 我来了
学习一下!
作者: 假装不在    时间: 2008-7-24 11:42
不管怎么D,我看不懂的,一定很不错,很深奥,我研究研究。




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2