如何使用Web浏览器控件填充html表单

时间:2012-05-26 15:20:21

标签: .net vb.net vb6

在VB6 classic中,我们可以这样做:

Private Sub Form_Load()
     WebBrowser1.Navigate2 "http://yourSite.com"
End Sub

Private Sub Command1_Click()
With Webbrowser1
     .Document.All("fieldName").Value = "some value"
     'click the button
     .Document.All("fieldName").Click
End With

End Sub

但是在VB.Net中,我们收到没有Value属性的错误,所以我尝试了:

  With wb
     ' fill From field
     .Document.All("fieldName").SetAttribute("Value", strFrom)
     ' click the button now
     .Document.All("fieldName").RaiseEvent("Click")
  End With

但即使这会产生错误:

Object reference not set to an instance of an object.

在线:

.Document.All("fieldName").SetAttribute("Value", strFrom)

如何在VB.net中做同样的事情?

1 个答案:

答案 0 :(得分:1)

我记得旧的VB6方式,如果你想在VB.Net代码中调用HTML元素的click事件:

Private Function ClickSubmitButton()

Dim theButton As HtmlElement

Try

' Link the ID from the web form to the Button var
theButton = webbrowser1.Document.GetElementById("loginbutton")

' Now do the actual click.
theButton.InvokeMember("click")
Return True

Catch ex As Exception

Return False

End Try

End Function

使用设置属性,请尝试以此为例(在我的脑海中):

Dim textArea = webBrowser1.Document.All("foo")
If textArea <> Nothing Then
textArea.InnerText = "This is a test"
End IF