Vb.net 无法使用mshtml.HTMLInputElement找到并单击提交按钮
下面是一个HTML表单,下面是一个vb程序“LoginExamp”,输入用户名和密码。但是,我无法找到按钮并单击它,因为它似乎没有显示为mshtml.HTMLInputElement。“htmlInput.click()”从不运行。如何调整loginExamp代码,以便单击按钮。谢谢你的帮助Vb.net 无法使用mshtml.HTMLInputElement找到并单击提交按钮,vb.net,visual-studio,browser,Vb.net,Visual Studio,Browser,下面是一个HTML表单,下面是一个vb程序“LoginExamp”,输入用户名和密码。但是,我无法找到按钮并单击它,因为它似乎没有显示为mshtml.HTMLInputElement。“htmlInput.click()”从不运行。如何调整loginExamp代码,以便单击按钮。谢谢你的帮助 <form id="loginform" name="loginform" method="post" action=""> <input id="username" class="for
<form id="loginform" name="loginform" method="post" action="">
<input id="username" class="formfield" type="text" value="User Name" maxlength="40" name="Xusername">
<input id="password" class="formfield" type="password" onfocus="clearDefault(this)" maxlength="40" name="Xpassword">
<button class="subButton" onclick="javascript: submitform()">submit!</button>
</form>
我终于找到了自己问题的答案。下面是有效的代码。输入用户名和密码,并单击按钮
Public Sub loginExamp()
Dim objIE As SHDocVw.InternetExplorer
Dim htmlDoc As mshtml.HTMLDocument
Dim htmlInput As Object
Dim url As String
url = "http://localhost/N1/ButtonClickTest.html"'test page with the loginform above
objIE = New SHDocVw.InternetExplorer
With objIE
.Navigate(url)
.Visible = True
While .Busy = True
Threading.Thread.Sleep(2000)
End While
htmlDoc = .Document
Dim htmlColl As mshtml.HTMLFormElement = DirectCast(htmlDoc.forms.item("loginform"), mshtml.HTMLFormElement)
While .Busy = True
Threading.Thread.Sleep(2000)
End While
For Each htmlInput In htmlColl
If htmlInput.name = "Xusername" Then
htmlInput.value = "theusername"
ElseIf htmlInput.name = "Xpassword" Then
htmlInput.value = "thepassword"
End If
If htmlInput.className = "subButton" Then
htmlInput.click()
End If
Next htmlInput
End With
End Sub
我看到您将按钮的类名更改为subButton。一开始是不同的;它总是子按钮吗?是的,这是示例代码中的一个错误,但不是问题的原因。事实上,在for下一个循环中,htmlInput.className是=“nothing”。您是否尝试过像使用输入元素一样使用名称,而不是使用class和className?在for each循环中,只找到两个项(两个循环),用户名和密码。因此,按钮似乎甚至不作为mshtml.HTMLInputElement的一部分存在。另外,按钮没有名字,只有一个类。你在哪里找到SHDocVw的?我在Windows\System32中使用它,但是我不断地遇到一些奇怪的错误,我想确保我使用的是正确的DLL。啊。。。根据这篇文章,我们需要以管理员的身份运行VisualStudio才能使其正常工作?这在部署应用程序时是如何实现的?
Public Sub loginExamp()
Dim objIE As SHDocVw.InternetExplorer
Dim htmlDoc As mshtml.HTMLDocument
Dim htmlInput As Object
Dim url As String
url = "http://localhost/N1/ButtonClickTest.html"'test page with the loginform above
objIE = New SHDocVw.InternetExplorer
With objIE
.Navigate(url)
.Visible = True
While .Busy = True
Threading.Thread.Sleep(2000)
End While
htmlDoc = .Document
Dim htmlColl As mshtml.HTMLFormElement = DirectCast(htmlDoc.forms.item("loginform"), mshtml.HTMLFormElement)
While .Busy = True
Threading.Thread.Sleep(2000)
End While
For Each htmlInput In htmlColl
If htmlInput.name = "Xusername" Then
htmlInput.value = "theusername"
ElseIf htmlInput.name = "Xpassword" Then
htmlInput.value = "thepassword"
End If
If htmlInput.className = "subButton" Then
htmlInput.click()
End If
Next htmlInput
End With
End Sub