我有下面的代码登录到网站中的代码。但是,登录按钮未被单击。我是Excel VBA新手,您有什么建议吗?
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub Myenter()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://willemendrees.nl/fn/login/"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.id_username.Value = "amsterdam@willemendrees.nl"
HTMLDoc.all.id_password.Value = "*****"
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
End With
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
发布于 2018-12-04 23:07:43
您可以使用[tabindex='3']
的CSS attribute = value选择器。这将通过值为3的tabindex
属性来确定登录按钮的目标。
HTMLDoc.querySelector("[tabindex='3']").Click
整件事:
Option Explicit
Public Sub AttemptLogin()
Dim IE As New InternetExplorer
With IE
.Visible = True
.Navigate2 "https://willemendrees.nl/fn/login/"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
With .querySelector("#id_username")
.Focus
.Value = "amsterdam@willemendrees.nl"
End With
With .querySelector("#id_password")
.Focus
.Value = "***"
End With
.querySelector("[tabindex='3']").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<== Delete me later
.Quit
End With
End Sub
发布于 2018-12-04 23:12:50
似乎html页面上有两个表单元素,而login是第二个。识别,然后提交表单。
HTMLDoc.all.id_username.Value = "amsterdam@willemendrees.nl"
HTMLDoc.all.id_password.Value = "*****"
HTMLDoc.getelementsbytagname("form")(1).submit
元素集合的索引号是从零开始的。
https://stackoverflow.com/questions/53615797
复制相似问题