我是VBA的新手。 我正在尝试在登录安全网站后单击"GO"搜索按钮。
我尝试了很多方法点击按钮,但我没有运气。
这是检查元素。
<button class="button primary pull-right" ng-click="search()" ng-disabled="searchForm.$invalid">
Go <i class="glyphicon glyphicon-chevron-right"></i>
</button>
这是我尝试过的。
方法 1
Set HTMLDoc = HTMLDoc.class("button primary pull-right").document
Set button = HTMLDoc.getElementsByTagName("BUTTON")(0) 'first button
button.Click
For i = 1 To 5
button.Click
DoEvents
Next
方法 2
For Each hyper_link In allhyperlinks
If hyper_link.class = "button primary pull-right" Then
hyper_link.Click
Exit For
End If
Next hyper_link
方法 3
Set allhyperlinks = IE.document.getElementsByTagName("button")
For Each hyper_link In allhyperlinks
If hyper_link.getAttribute("class") = "search" Then
hyper_link.Click
Exit For
End If
Next
方法 4
Dim oHTML_Element As IHTMLElement
Dim oBrowser As InternetExplorer
Dim ie As Variant
For Each oHTML_Element In ie.document.getElementsByName("button")
If oHTML_Element.className = "button primary pull-right" Then
oHTML_Element.Click
End If
Next
方法 5
Set ie = CreateObject("InternetExplorer.Application")
With ie
Do Until .readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:15"))
ie.document.getElementsByClassName("button primary pull-right").Click
Application.Wait (Now + TimeValue("0:00:1"))
End With
方法 6
Set ie = CreateObject("InternetExplorer.Application")
ie.document.getElementByClassName("button primary pull-right")(0).Click
任何帮助将不胜感激。
谢谢!
我认为您没有提供网址,因为它超出了登录范围。 这使得解决难度增加十倍。 不过有几点。
- ng-Click表示其正在运行的Angular。
- 有时,需要在模拟 Click 事件之前使控件获得焦点。 网站无法识别我的输入[如何从VBA手动触发IE dom事件]
- 我会使用 .querySelector(( 或 .querySelectorAll(( 来获取按钮,因为我可以使用 Chrome 开发工具窗口底部的路径。 VBA - Webscraping - jQuery selectors with MSHTML 的 querySelector and querySelectorAll
试试这段代码
Sub Test()
Dim ie As Object
Dim e As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate "www.jetblue.com"
Do Until .readyState = 4: DoEvents: Loop
For Each e In .document.getElementsByTagName("input")
If e.ID = "email_field" Then
e.Value = "your email"
ElseIf e.ID = "password_field" Then
e.Value = "your password"
End If
Next e
For Each e In .document.getElementsByTagName("input")
If e.ID = "signin_btn" And e.Type = "submit" Then e.Click: Exit For
Next e
End With
End Sub