VBA(Instagram如何单击“跟随”按钮)?

时间:2019-06-11 14:16:42

标签: excel vba web-scraping webautomation

我正在使用Office2019。在VBA中,我尝试单击“跟随”按钮。在INSTAGRAM上,但没有点击。只需打开页面,然后什么都不会发生。 因此,请帮助我,因为这是我项目的重点。

Sub one()


Dim IE As Object
Dim doc As HTMLDocument
Dim ele As IHTMLElement


Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.navigate "https://www.instagram.com/gal_gadot/"

Do
Loop Until IE.readyState = 4

Set doc = IE.document

'doc.getElementsByTagName("button").innerText
'doc.getElementsByClassName("_5f5mN       jIbKX  _6VtSN     yZn4P   ").innerText

For Each ele In doc.getElementsByTagName("span")
   ' If InStr(ele.innerText, "Follow") > 0 Then ele.Click
    On Error Resume Next
    If ele.Value = "Follow" Then
    ele.Click
    End If
Next

For Each ele In doc.getElementsByTagName("button")
   ' If InStr(ele.innerText, "Follow") > 0 Then ele.Click
    On Error Resume Next
    If ele.Value = "Follow" Then
    ele.Click
    End If
    Next


End Sub

这里是HTML文件

<span class="BY3EC  bqE32">
<span class="vBF20 _1OSdk">
<button class="_5f5mN       jIbKX  _6VtSN     yZn4P   ">Follow</button></span>
<span class="mLCHD _1OSdk">
<button class="_5f5mN       jIbKX KUBKM      yZn4P  m4t9r ">
<div class="OfoBO">
<div class="_5fEvj coreSpriteDropdownArrowWhite">
</div>
</div>
</button>
</span>
</span>

2 个答案:

答案 0 :(得分:0)

该脚本将帮助您单击该关注按钮,但是只有在登录后才能关注她,因为单击该按钮时,它将引导您进入登录页面。顺便说一句,您应该始终定义任何定时循环,以便在元素不可用的情况下可以退出do loop。 SO中有几篇文章都对此进行了介绍。

Sub FollowGalGadot()
    Const Url$ = "https://www.instagram.com/gal_gadot/"
    Dim IE As New InternetExplorer, post As Object

    With IE
        .Visible = True
        .navigate Url
        While .Busy = True Or .readyState < 4: DoEvents: Wend

        Do
            DoEvents
            On Error Resume Next
            Set post = .document.querySelectorAll("a[href*='follow'] > button")
            On Error GoTo 0
        Loop While post.Length = 0

        post.item(0).Click

    End With
    Stop
    IE.Quit
End Sub

答案 1 :(得分:0)

谢谢你,SIM卡!我不是程序员,但做了一些改动,效果很好!

Sub FollowGalGadot()
    Const Url$ = "https://www.instagram.com/gal_gadot/"
    Dim IE As New InternetExplorer, post As Object

    With IE
        .Visible = True
        .navigate Url
        While .Busy = True Or .readyState < 4: DoEvents: Wend

        While post Is Nothing
            On Error Resume Next
            Application.Wait Now + TimeValue("00:00:001")
            Set post = .document.querySelector("._5f5mN.jIbKX._6VtSN.yZn4P")
            If Not post Is Nothing Then
                post.Click
                Debug.Print "it's done!!!"
            Else:
                Debug.Print "trying again"
            End If
            On Error GoTo 0
        Wend

    End With
    Stop
    IE.Quit
End Sub