VBA代码单击网页中的“播放活动”按钮

时间:2017-10-06 06:19:41

标签: excel vba

我正在寻求一些帮助,我可以点击“播放活动”按钮"在我们办公室的全球生产力中心使用excel VBA。我正在用午餐和休息跟踪器创建一个考勤登录工具。员工单击使用我创建的工具登录后,GPH站点将启动并将其登录,与Log Out相同。但是,我使用我创建的工具很难控制GPH网站的休息和午餐按钮。我找不到与我的vba代码集成的正确元素。我希望有人可以帮助我。请参阅下面的特定按钮和图像的HTML代码。



<tr class="top-five">
    <td title="Play Activity" class="glyphicon glyphicon-play-circle play-activity" data-toggle="tooltip" data-team-id="e9575e8b-4682-734c-b28d-b1ee5532a8ce" data-activity-id="e0728017-a4fa-ef4b-91a6-262e5418a134" data-team-name="Technology Team"> </td>
 <td> 
   <span title="Break Activities" class="act-ellipsis" data-toggle="tooltip">
<span class="ellip ellip-line"> Break Activities </span>

</span>
 Break
</td> 

<td title="Favorite" class="glyphicon glyphicon-star" data-toggle="tooltip" data-activity-id="e0728017-a4fa-ef4b-91a6-262e5418a134"></td>

</tr>
&#13;
&#13;
&#13;

Image

2 个答案:

答案 0 :(得分:0)

您的屏幕截图太小,无法看到任何细节,但基本上,它的工作原理如下。

Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()

Dim oHTML_Element As IHTMLElement
Dim sURL As String

On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

HTMLDoc.all.Email.Value = "sample@vbadud.com"
HTMLDoc.all.passwd.Value = "*****"

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub

确保设置对...的引用

Microsoft HTML Object Library
Microsoft Internet Controls

答案 1 :(得分:0)

您可以使用CSS选择器选择Break Activities元素:

span[title='Break Activities']

该标签读为title标记内的值为"Break Activities"的属性span


正在使用CSS查询:

CSS query


VBA:

IE.document.querySelector("span[title='Break Activities']").Click

概述实施:

Option Explicit
Public Sub GetInfo()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .navigate "yourURL"

        While .Busy Or .readyState < 4: DoEvents: Wend '<== Loop until loaded

        'Application.Wait Now + TimeSerial(0,0,3) '< Additional wait for element to become avaiable may be required
         .document.querySelector("span[title='Break Activities']").Click

        'Quit '<== Remember to quit application
    End With

End Sub

.querySelector方法:

Document方法querySelector()返回文档中与指定选择器或选择器组匹配的第一个Element。如果没有找到匹配项,则返回null。

语法:element = document.querySelector(selectors); <==注意“;”在VBA中未使用