用webbrowser打开网页后,网页内的所有元素你都可以通过代码来操作,

方法是:

1.根据标记名(tagname)的和元素名name来找到元素,

2.给元素赋值或是执行相关的事件.

例1: 给username文本框内填充内容:

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)

Dim doc

Dim tg

Set doc = WebBrowser1.Document

For i = 0 To doc.All.length - 1

If (LCase(doc.All(i).tagname)) = "input" Then

if (LCase(doc.All(i).name)) = "username" then

Set tg = doc.All(i)

tg.value=text1.text

end if

End If

Next i

End Sub

例2: 找到提交按钮并点击

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)

Dim doc

Dim tg

Set doc = WebBrowser1.Document

For i = 0 To doc.All.length - 1

If (LCase(doc.All(i).tagname)) = "input" Then

if (LCase(doc.All(i).type)) = "submit" then

Set tg = doc.All(i)

tg.click

end if

End If

Next i

End Sub