如何填写没有id的文本框

时间:2013-10-23 13:39:54

标签: asp.net vb.net

我正在尝试在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

1 个答案:

答案 0 :(得分:3)

由于您无法控制您尝试自动化的来源,因此我建议使用WatiN等库来帮助您选择要使用的项目,例如:

以下是使用WatiN访问Google并进行搜索的示例:

' 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等查找元素,然后对元素执行操作。