在VBA中使用.value进行Internet Explorer自动化的问题

时间:2017-02-05 14:01:09

标签: vba excel-vba internet-explorer excel

我有这个代码登录Instagram页面。它捕获用户名字段的标签名称并在其上输入一个值(我想要登录的Instagram的用户名)。

Instagram代码页:

input name="username" class="_kp5f7 _qy55y" aria-required="true" aria-describedby="" aria-label="Nome de usuário" type="text" maxlength="30" placeholder="Nome de usuário" value="" autocapitalize="off" autocorrect="off"

完整代码(由用户编辑,无法编辑帖子)

Sub FOLLOW()

Dim IE As Object

Set IE = CreateObject("InternetExplorer.Application")

Dim contador, contador2, contador3, lngremoto, turnopar As Integer
Dim numeropages, xopepacounter, usuario, senha, numeroseguir, tipopage As String
Dim numeropaginasseguir, entrarpage As String

usuario = Cells(2, 2).Value
senha = Cells(2, 3).Value

'abrir o IE
IE.Visible = True
apiShowWindow IE.Hwnd, SW_MAXIMIZE

'maximizar, caso minimizado
IE.navigate ("www.instagram.com")

'Wait for ie to LOAD
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop
Sleep 1500

'Click on switch accounts
'Set inputcollection = IE.document.getElementsByTagName("a")
'For Each aelement In inputcollection
   ' If aelement.getAttribute("class") = "_niffh" Then
        'If InStr(aelement.innerText, "Trocar de conta") Then
         'aelement.Click
         'End If
    'End If
'Next

'Click on login button
Set inputcollection = IE.document.getElementsByTagName("a")
For Each aelement In inputcollection
    If aelement.getAttribute("class") = "_fcn8k" Then
        If InStr(aelement.innerText, "Fa?a login") Then
         aelement.Click
         End If
    End If
Next

'Wait for IE to load
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Sleep 3000

'Fill the field username
Set inputcollection = IE.document.getElementsByTagName("input")
For Each aelement In inputcollection
    If aelement.getAttribute("name") = "username" Then
         aelement.Focus
         aelement.Value = usuario
         Sleep 1000
    End If
Next

'Fill the field password
Set inputcollection = IE.document.getElementsByTagName("input")
For Each aelement In inputcollection
    If aelement.getAttribute("name") = "password" Then
         aelement.Focus
         aelement.Value = senha
         Sleep 1000
    End If
Next

'click on sign in
Set inputcollection = IE.document.getElementsByTagName("button")
For Each aelement In inputcollection
    If aelement.getAttribute("class") = "_ah57t _84y62 _7xso1 _rmr7s" Then
        If InStr(aelement.innerText, "Entrar") Then
         aelement.Click
         End If
    End If
Next

End Sub

问题是代码没有正确填充字段。看起来它看起来像填写在Instagram页面上,但当你点击登录消息时出现错误,显示我没有填写用户名字段。

此代码仅适用于send keys方法,但对我来说无效,因为我想隐藏Internet Explorer窗口。

有什么建议吗?

0 个答案:

没有答案