例如WebBrowser中已显示有网页http://www.csdn.net,有什么方法可以控制WebBrowser控件中的元素,列表框/文本域等,谢谢

解决方案 »

  1.   

    控制网页的FORMS行为Private Sub Command2_Click()
        With WebBrowser1.Document.Forms(0)
            .c2.Checked = 1
            .r1(1).Checked = 1
        End With
    End Sub
    Private Sub Command2_Click()
        With WebBrowser1.Document.Forms(0)
            .d1.Options(1).Selected = 1
        End With
    End Subweb.Document.getElementsByName("D1").Item(0).selectedIndex = 1==============================================
    <input type="radio" value="n" checked name="notecome">普通
    <input type="radio" value="c" name="notecome">原创
    <input type="radio" value="z" name="notecome">转帖
    <input type="button" value="发送提交" name="button"比如一个网页里有如上代码
    我想选择原创
    webbrowser中怎么写
    Private Sub Command1_Click()
        WebBrowser1.Navigate "c:\ggg.html"
    End SubPrivate Sub Command2_Click()
        Dim x
        
        For Each x In WebBrowser1.Document.All("notecome")
            If x.Value = "c" Then
                x.Checked = True
            End If
        Next
    End Sub============================================================================================
    假设你的HTML代码如下:<html>
    <script>
      function abcd(){
        alert("haha");
        return false;
      }
    </script><body>
      <a id = 'xxx' href=# onclick="abcd()">ggggg</a>
    </body>
    </html>VB代码如下:
    Private Sub Command1_Click()
        WebBrowser1.Navigate "http://www.applevb.com/script_test.html"
    End SubPrivate Sub Command2_Click()
        Dim a, b
        Dim d As IHTMLDocument2
        
        For Each a In WebBrowser1.Document.All
            Debug.Print a.tagName
            If (a.tagName = "SCRIPT") Then        End If
            If (a.tagName = "A") Then
                If a.Id = "xxx" Then
                    a.FireEvent ("onclick")
                End If
            End If
        Next点击Command1浏览这个网页,点击Command2运行其中的脚本abcd。
    ==============================================怎么编程把用户名,密码提交到网页上的登录页?
    首先在程序中加入Webbrowser控件并加入引用 Microsoft HTML Object Library。
    假设你的HTML页面表单代码如下:
    <form method="POST" action="http://chen/dll/chat/chatmain.exe/RegUser">
      <p>请填写下面表单注册(*项为必添项)</p>
      <p>*姓名<input type="text" name="Name" size="20"></p>
      <p>*昵称<input type="text" name="NickName" size="20"></p>
      <p>电子邮件<input type="text" name="EMail" size="20"></p>
      <p>*密码<input type="text" name="Password" size="20"></p>
      <p><input type="submit" value="提交" name="B1"><input type="reset" value="全部重写" name="B2"></p>
    </form>
    注意其中元素的type、Name、value属性。然后VB中的代码如下:
    Private Sub Command1_Click()
        WebBrowser1.Navigate "http://chen/chat/newuser.htm"
    End SubPrivate Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
        Dim vDoc, vTag
        Dim i As Integer
          
        Set vDoc = WebBrowser1.Document
        List1.Clear
        For i = 0 To vDoc.All.length - 1
            If UCase(vDoc.All(i).tagName) = "INPUT" Then
                Set vTag = vDoc.All(i)
                If vTag.Type = "text" Or vTag.Type = "password" Then
                    List1.AddItem vTag.Name
                    Select Case vTag.Name
                        Case "Name"
                            vTag.Value = "IMGod"
                        Case "NickName"
                            vTag.Value = "IMGod"
                        Case "Password"
                            vTag.Value = "IMGodpass"
                        Case "EMail"
                            vTag.Value = "[email protected]"
                    End Select
                ElseIf vTag.Type = "submit" Then
                    vTag.Click
                End If
            End If
        Next i
    End Sub
    点击Command1就可以自动填表并提交了。 
    =====================================================================================
    调用forms下的Submit控件的Click事件,我会做,但我不想这么做.有没有办法直接调用类似于:web1.document.forms.submit,这句语句我怎么写都不成功
    是这个
    Webbrowser1.document.formName.submit()不能用,formname为form1所以我调用Webbrowser1.document.form1.submit
    出错类型:对象不支持该属性或方法,
    然后调用Webbrowser1.document.forms(0).submit()
    出错类型同上
    Private Sub Command1_Click()
        WebBrowser1.Navigate "http://localhost/webapplication2/MyLogonPage.aspx"
    End SubPrivate Sub Command2_Click()
        WebBrowser1.Document.All("Form1").submit
    End Sub
    <form name="form1" method="post" action="aa.asp">
    ......
    <input name="reset" type="reset" vlaue="reset" class="button">
    </form>
    我本想把reset的type改成submit 再提交,可出错,type是只读属性,不能修改,我只要有办法把这页面递交出去就行,当然,用POST也不行,参数太多,组合方式太多
    你用下面的代码试一下你的页面:
    Private Sub Command1_Click()
        WebBrowser1.Navigate "http://oakhome.xicp.net/webapplication2/MyLogonPage.aspx"
    End SubPrivate Sub Command2_Click()
        Dim x
        
        On Error Resume Next
        For Each x In WebBrowser1.Document.All
            List1.AddItem x.Name
        Next
    End Sub看看在List1里面列出来的页面元素的名字有没有Form1
    找到原因了,你的页面是这样的:
    <input language="javascript" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " name="Submit1" id="Submit1" type="submit" value="Submit" />
    你把name="Submit1" 改成name="Submit"肯定就不会成功了,很不幸的是我要提交的页面中就有这样一句,现在可有办法解决吗???=======================================================================
    使用WebBrowser_V1接受消息Private WithEvents WebMessage As WebBrowser_V1Private Sub Form_Load()
      Set WebMessage = WebBrowser1.Object
    End SubPrivate Sub WebMessage_NewWindow(ByVal URL As String, ByVal Flags As Long, ByVal TargetFrameName As String, PostData As Variant, ByVal Headers As String, Processed As Boolean)'这里有Flags变量可以取得窗体应有的状态End Sub具体值需要你自己去试试看。对象浏览器里面没有=======================================================================================================通过下面的方法遍历页面中的IFrame:Sub EnumFrames(ByVal wb As WebBrowser)
    Dim pContainer As olelib.IOleContainer
    Dim pEnumerator As olelib.IEnumUnknown
    Dim pUnk As olelib.IUnknown
    Dim pBrowser As SHDocVw.IWebBrowser2   Set pContainer = wb.Object.Document
       
       ' Get an enumerator for the frames
       If pContainer.EnumObjects(OLECONTF_EMBEDDINGS, pEnumerator) = 0 Then
       
          Set pContainer = Nothing
          
          ' Enumerate and refresh all the frames
          Do While pEnumerator.Next(1, pUnk) = 0
             
             On Error Resume Next
             
             ' Clear errors
             Err.Clear
             
             ' Get the IWebBrowser2 interface
             Set pBrowser = pUnk
       
             If Err.Number = 0 Then
                Debug.Print "Frame: " & pBrowser.LocationURL
             End If
       
          Loop
          
          Set pEnumerator = Nothing
       
       End If
       
    End Sub
    =======================================================================================
      

  2.   

    转载看到很多关于WebBrowser控件禁止右键的提问,回复的方法很多,其中有提到使用微软提供的Webbrowser扩展COM服务器对象(WBCustomizer.dll),但是该方法在我们想使用Webbrowser编辑网页(Webbrowser1.Document.execCommand "editMode")的时候有很多弊端,比如不能显示选中的文本等。另有些方法也就不用一一列举了。这儿我想提到的是关于MSHTML.HTMLDocument引用Microsoft HTML OBject LibraryRem #窗体代码#Dim WithEvents M_Dom As MSHTML.HTMLDocument 
    Private Function M_Dom_oncontextmenu() As Boolean
            M_Dom_oncontextmenu = False
    End Function Private Sub Webbrowser1_DownloadComplete()
          Set M_Dom = Webbrowser1.Document
     End SubRem 好了,右键菜单没有了===============================================================================控件调用和获得收藏夹里面基本上用 specialfolder(6 ) 就可以得到收藏夹的路径, 然后你可以用dir去循环读入每个目录,然后dir里面的file, file的名字就是你要的收藏的名字, 路径可以自己根据从上面得到的路径去得到.
    如果你不用dir也可以用vb的dir控件.
    Private Type SHITEMID
        cb As Long
        abID As Byte
    End TypePublic Type ITEMIDLIST
        mkid As SHITEMID
    End Type
    Public Function SpecialFolder(ByRef CSIDL As Long) As String
        'locate the favorites folder
        Dim R As Long
        Dim sPath As String
        Dim IDL As ITEMIDLIST
        Const NOERROR = 0
        Const MAX_LENGTH = 260
        R = SHGetSpecialFolderLocation(MDIMain.hwnd, CSIDL, IDL)
        If R = NOERROR Then
            sPath = Space$(MAX_LENGTH)
            R = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath)
            If R Then
                SpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1)
            End If
        End If
    End Function
    ===================================================================================================
    全屏是的,webbrowser本生是一个控件, 你要它全屏,就是要它所在的窗体全屏, 可以用setwindowlong取消窗体的 title, 用Call ShowWindow(FindWindow("Shell_traywnd", ""), 0) 隐藏tray,就是下边那个包含开始那一行. 用Call ShowWindow(FindWindow("Shell_traywnd", ""), 9) 恢复. 够详细了吧.然后在form1.windowstate = 2 就可以了.==============================================================================================================
    选择网页上的内容。
    Private Sub Command1_Click()
    '请先选中一些内容
    Me.WebBrowser1.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT
    MsgBox Clipboard.GetText
    End Sub==============================================================================================================
    用IE来下载文件
    Private Declare Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long
    Private Sub Command1_Click()
       
       Dim sDownload As String
       
       sDownload = StrConv(Text1.Text, vbUnicode)
       Call DoFileDownload(sDownload)
       
    End SubPrivate Sub Form_Load()
    Text1.Text = "http://www.chat.ru/~softdaily/fo-ag162.zip"
    Form1.Caption = "Audiograbber 1.62 Full"
    Text2.Text = "http://www6.50megs.com/audiograbber/demos/cr-ag161.zip"
    End Sub
    ================================================================================================================我要动态加载和删除WebBrowser控件应该怎么做?Private Sub Command1_Click()
       Form1.Controls.Add "shell.explorer.2", "NewWeb", Form1
        With Form1!NewWeb
            .Visible = True
            .Width = 10000
            .Height = 10000
            .Left = 0
            .Top = 0
            .Navigate2 "www.csdn.net"
        End With
    End SubPrivate Sub Command2_Click()
         Controls.Remove Form1!newweb
    End SubForm1.Controls.Add "shell.explorer.2", newweb(newweb.Count), Form1
        With Form1!newweb(newweb.Count)
            .Visible = True
            .Width = 1000
            .Height = 1000
            .Left = newweb(newweb.Count - 1).Left + newweb(newweb.Count - 1).Width
            .Top = 0
            '.Navigate2 "www.csdn.net"
        End With
    为什么他说我
    Form1.Controls.Add "shell.explorer.2", newweb(newweb.Count), Form1
    这一行错误13 类型不匹配?
    ps:我在form中已经有了一个newweb(0)控件先为一个WebBrowser
    Dim i As Integer
    Private Sub AddWeb_Click()
        For i = 1 To 10
            Load NewWeb(i)
            NewWeb(i).Top = i * 100
            NewWeb(i).Left = i * 100
            NewWeb(i).Visible = True
        Next i
    End SubPrivate Sub DelWeb_Click()
        For i = 1 To 10
            Unload NewWeb(i)
        Next i
    End Sub=============================================================================================================
    一个把页面保存为MHT(即MHTML)文件
    1、谢谢楼上几位大侠!我现在将 pcwak(书剑狂生[MS MVP]) 大侠提供的资料贴出来,以供大家参考:
     我终于找到一个把页面保存为MHT(即MHTML)文件的方法了!
    首先,在工程中必须要引用一个库:
    Library CDO
    D:\WINNT\System32\cdosys.dll
    Microsoft CDO for Windows 2000 Library
    其次,需要Stream对应的接口的支持,如果你一时找不到就使用支持这个的较新的ADO就行了,如
    Library ADODB
    D:\Program Files\Common Files\system\ado\msado15.dll
    Microsoft ActiveX Data Objects 2.5 Library
    代码如下,十分简单(同时由于流的特点,显示在实际应用中没必要象本例中那样把文件保存到磁盘上就可直接转换为字符串或字节数组什么的处理的。另,对于Microsoft CDO for Windows 2000 Library这个库,在WIN98中要怎么使用还没试过,感兴趣的朋友可以试试并跟帖,以丰富完善其内容:)Private Sub Command1_Click()
    ' Reference to Microsoft ActiveX Data Objects 2.5 Library
    ' Reference to Microsoft CDO for Windows 2000 Library
    Dim iMsg As New CDO.Message
    Dim iConf As New CDO.Configuration
    Dim objStream As ADODB.StreamWith iMsg
    .CreateMHTMLBody "http://www.163.com/";, , _
    "domain\username", _
    "password"
    Set objStream = .GetStream
    Call objStream.SaveToFile("f:\test.mht", adSaveCreateOverWrite)
    End With
    End Sub2、
    '首先加入对ADODB和CDO(Microsoft CDO for Windows 2000 Library,对应文件名为CDOSYS.dll)的引用
    Private Sub Command1_Click()
        Dim message As New CDO.message
        Dim Outstream As ADODB.Stream
        On Error GoTo myerr1
        Call message.CreateMHTMLBody("http://www.csdn.net", CDO.CdoMHTMLFlags.cdoSuppressNone, "", "")
        Set Outstream = message.GetStream
        Call Outstream.SaveToFile("c:\test.mht", ADODB.SaveOptionsEnum.adSaveCreateOverWrite)
        MsgBox "完成"
        
        Exit Sub
    myerr1:
        Set message = Nothing
        Set Outstream = Nothing
    End Sub=================================================================================================================
    请问高手们怎样在WebBrowser控件调用收藏夹和在收藏夹里添加收藏
    Option ExplicitPrivate Sub Command1_Click()
        Dim ObjSUH As New ShellUIHelper
        ObjSUH.AddFavorite "http://www.csdn.net", "CSDN"
        Set ObjSUH = Nothing
    End Sub=================================================================================================================