51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 6014|回复: 9
打印 上一主题 下一主题

[原创] 学习VB.net对IE的控制一(希望对大家有点帮助)

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2008-2-29 02:25:58 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
'首先在工程中加入对
    '"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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

10#
发表于 2008-7-24 11:42:35 | 只看该作者
不管怎么D,我看不懂的,一定很不错,很深奥,我研究研究。
回复 支持 反对

使用道具 举报

该用户从未签到

9#
发表于 2008-7-24 11:11:36 | 只看该作者

我来了

学习一下!
回复 支持 反对

使用道具 举报

该用户从未签到

8#
发表于 2008-3-8 16:08:02 | 只看该作者
学习学习
回复 支持 反对

使用道具 举报

该用户从未签到

7#
发表于 2008-3-8 14:56:51 | 只看该作者
原帖由 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 ...

不错,支持一下!@
回复 支持 反对

使用道具 举报

该用户从未签到

6#
 楼主| 发表于 2008-3-8 14:35:52 | 只看该作者

用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
回复 支持 反对

使用道具 举报

该用户从未签到

5#
 楼主| 发表于 2008-3-8 14:33:14 | 只看该作者
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 编辑 ]
回复 支持 反对

使用道具 举报

该用户从未签到

4#
发表于 2008-3-1 16:44:35 | 只看该作者
又学到了点东东 谢谢分享!
回复 支持 反对

使用道具 举报

  • TA的每日心情
    开心
    2016-2-27 08:48
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]测试小兵

    3#
    发表于 2008-2-29 20:36:58 | 只看该作者
    非常感谢楼主的共享!
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    2#
     楼主| 发表于 2008-2-29 02:33:56 | 只看该作者
    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
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-22 05:43 , Processed in 0.074886 second(s), 26 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表