0

我正在尝试自动化 VB.net 中的网页,但我无法在文本框中填写值,我的代码只能单击按钮

网页HTML如下

<TD align=left height="25" width="350">&nbsp;&nbsp;
    <input type="text" name="UserName" size="20">
</TD>
<TD align=left height="25" width="350">&nbsp;&nbsp;
    <input type="password" name="Password1" size="20" maxlength="12">
</TD> 
<TD align=left height="25" width="350">&nbsp;
    <input type="button" onClick="UserVer();" value="Submit"name="BLogin">
</TD> 

我的 VB.net 代码如下:

Dim doc As HtmlDocument = wc.Document
Dim link As HtmlElement
Dim links As HtmlElementCollection = wc.Document.All
Dim dom = doc.GetElementsByTagName("a")
Dim t As HtmlElement = wc.Document.All(Name)

For Each link In links
    If link.GetAttribute("name") = "UserName" Then
        link.SetAttribute("value", UNameTxt.Text)
    End If
Next
For Each link In links
    If link.GetAttribute("name") = "Password1" Then
        link.SetAttribute("value", UPassTxt.Text)
    End If
Next
For Each link In links
    If link.GetAttribute("value") = "Submit" Then

        link.InvokeMember("click")

        Exit Sub
    End If
Next
4

1 回答 1

3

由于您无法控制尝试自动化的源,因此我建议使用WatiN 之类的库来帮助选择您希望使用的项目,如下所示:

这是一个使用 Google 进行搜索的示例WatiN

' Go to URL for Google search
Dim browser As New IE("http://www.google.com/")

' Find the search text box by name and type text `WatiN` in the box
browser.TextField(Find.ByName("q")).TypeText("WatiN")

' Find the search button by name and click the button
browser.Button(Find.ByName("btnG")).Click()

您可以按名称、样式、ID 等查找元素,然后对元素执行操作。

于 2013-10-23T13:58:06.947 回答