gpt4 book ai didi

vb.net - 无法使用 mshtml.HTMLInputElement 找到并单击提交按钮

转载 作者:行者123 更新时间:2023-12-03 18:42:40 25 4
gpt4 key购买 nike

下面是一个 HTML 表单,下面是一个输入用户名和密码的 vb 程序“LoginExamp”。但是,我无法找到该按钮并单击它,因为它似乎没有显示为 mshtml.HTMLInputElement。 “htmlInput.click()”永远不会运行。如何调整 loginExamp 代码以便单击该按钮。谢谢你的帮助。

<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 mshtml.HTMLInputElement
Dim htmlColl As mshtml.IHTMLElementCollection
Dim url As Object
url = "http://localhost/ButtonClickTest.html" 'just a 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
htmlColl = htmlDoc.getElementsByTagName("INPUT")
While .Busy = True
Threading.Thread.Sleep(2000)
End While
For Each htmlInput In htmlColl
If htmlInput.name = "Xusername" Then
htmlInput.value = "theusername" 'this works
ElseIf htmlInput.name = "Xpassword" Then
htmlInput.value = "thepassword" 'This works too
End If
If htmlInput.className = "subButton" Then 'This is never true
htmlInput.click() 'This line never runs
End If
Next htmlInput
End With
End Sub

最佳答案

我终于能够找出我自己问题的答案。下面是有效的代码。输入用户名和密码并单击按钮。

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

关于vb.net - 无法使用 mshtml.HTMLInputElement 找到并单击提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9088120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com