使用excel VBA单击IE中的Angular(ng-click)搜索按钮

时间:2018-03-09 23:01:52

标签: vba excel-vba automation getelementsbyclassname getelementsbyname

我是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

非常感谢任何帮助。

谢谢!

2 个答案:

答案 0 :(得分:1)

我认为你没有给出url,因为它超出了登录名。这使解决困难十倍。不过有些观点。

答案 1 :(得分:0)

试试此代码

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