<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 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页面表单代码如下:
<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 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就可以自动填表并提交了。
不能用,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
<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 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
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)
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
Private Sub Form_Load()
WebBrowser1.Navigate "http://test/index.html"
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim sTemp As String
Dim outStr As String
Dim i As Integer
Dim beginPos As Long
Dim endPos As Long
sTemp = WebBrowser1.Document.documentelement.InnerHTML
'Text1.Text = sTemp
i = 1
Do While i <> 0
i = InStr(1, sTemp, "<SCRIPT")
If i <> 0 Then
outStr = Left(sTemp, i - 1)
sTemp = Right(sTemp, Len(sTemp) - i - 6)
i = InStr(1, sTemp, "</SCRIPT>")
If i <> 0 Then
sTemp = Right(sTemp, Len(sTemp) - i - 8)
End If
sTemp = outStr & sTemp
End If
Loop
WebBrowser1.Document.write sTemp
'Text2.Text = sTemp
End Sub