控制网页的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 Sub
web.Document.getElementsByName("D1").Item(0).selectedIndex = 1
==============================================
普通
原创
转帖
比如一个网页里有如上代码
我想选择原创
webbrowser中怎么写
Private Sub Command1_Click()
WebBrowser1.Navigate "c:\ggg.html"
End Sub
Private 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代码如下:
function abcd(){
alert("haha");
return false;
}
ggggg
VB代码如下:
Private Sub Command1_Click()
WebBrowser1.Navigate "http://www.applevb.com/script_test.html"
End Sub
Private 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页面表单代码如下:
请填写下面表单注册(*项为必添项)
*姓名
*昵称
电子邮件
*密码
注意其中元素的type、Name、value属性。然后VB中的代码如下:
Private Sub Command1_Click()
WebBrowser1.Navigate "http://chen/chat/newuser.htm"
End Sub
Private 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 = "IMGod@paradise.com"
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 Sub
Private Sub Command2_Click()
WebBrowser1.Document.All("Form1").submit
End Sub
......
我本想把reset的type改成submit 再提交,可出错,type是只读属性,不能修改,我只要有办法把这页面递交出去就行,当然,用POST也不行,参数太多,组合方式太多
你用下面的代码试一下你的页面:
Private Sub Command1_Click()
WebBrowser1.Navigate "http://oakhome.xicp.net/webapplication2/MyLogonPage.aspx"
End Sub
Private 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
找到原因了,你的页面是这样的:
你把name="Submit1" 改成name="Submit"肯定就不会成功了,很不幸的是我要提交的页面中就有这样一句,现在可有办法解决吗???
=======================================================================
使用WebBrowser_V1接受消息
Private WithEvents WebMessage As WebBrowser_V1
Private Sub Form_Load()
Set WebMessage = WebBrowser1.Object
End Sub
Private 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
本文介绍了如何在VB中使用WebBrowser控件打开本地HTML文件,并进行DOM操作,如设置表单元素状态。同时展示了如何触发HTML中的JavaScript函数,包括通过FireEvent方法模拟点击事件。此外,还探讨了如何处理表单提交问题,以及遍历页面中的IFrame。内容涵盖了VB与HTML、JavaScript的交互以及WebBrowser控件的深入应用。
2950

被折叠的 条评论
为什么被折叠?



