首页 > 解决方案 > 无法使用宏和 vba 登录网站

问题描述

我正在尝试使用以下适用于不同网址的网站登录

Sub Mylogin()

    Dim MyHTML_Element As IHTMLElement
    Dim MyURL As String
    On Error GoTo Err_Clear
    MyURL = "MYURL"
    ie.Silent = True
    ie.Navigate MyURL
    ie.Visible = True
    Do
    Loop Until ie.ReadyState = READYSTATE_COMPLETE
        Set HTMLDoc1 = ie.Document
        HTMLDoc1.all.Email.Value = "MYEMAIL" 'Enter your email id here
        HTMLDoc1.all.Password.Value = "MYPASSWD" 'Enter your password here
        For Each MyHTML_Element In HTMLDoc1.getElementsByTagName("input")
            If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
        Next
        Err_Clear:
        If Err <> 0 Then
            Err.Clear
            Resume Next
        End If

End Sub

提交按钮在 div 下,我不知道这是否是问题所在。html登录部分如下

<div id="login-area-main">
       <div id="user">
            <div id="username">
            <input name="p$lt$ctl01$LogonFormIndice$loginElem$UserName" type="text" maxlength="50" id="p_lt_ctl01_LogonFormIndice_loginElem_UserName" placeholder="E-mail" />
            <span class="CMSValidator"><span id="p_lt_ctl01_LogonFormIndice_loginElem_rfvUserNameRequired" title="Please enter a user name." class="profile-validator validator error-message" style="display:none;">
            </span></span>
            </div>
            </div>
            <div id="pass">
            <div id="password">
            <input name="p$lt$ctl01$LogonFormIndice$loginElem$Password" type="password" maxlength="20" id="p_lt_ctl01_LogonFormIndice_loginElem_Password" placeholder="Password" />
            <span class="CMSValidator"><span id="p_lt_ctl01_LogonFormIndice_loginElem_rfvPasswordRequired" class="profile-validator validator error-message" style="visibility:hidden;">
            </span></span>
            </div>
            </div>
            <div id="pass-forgot">
            <p><a href="/Special-Pages/ForgotPassword">Forgot Password</a></p>
            </div>
            <div id="submit-button">
            <a id="p_lt_ctl01_LogonFormIndice_loginElem_btnLogon" class="buyBtn button" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;p$lt$ctl01$LogonFormIndice$loginElem$btnLogon&quot;, &quot;&quot;, true, &quot;p_lt_ctl01_LogonFormIndice_MiniLogon&quot;, &quot;&quot;, false, true))"></a>
            </div>

我必须做些什么吗

MyHTML_Element.Type = "submit"

并放入 div id 中?

我注意到 HTMLDoc1 是空的..!它应该是空的吗?

我尝试了以下但没有显示或单击

Const Url$ = "URL"

Dim UserName As String, Password As String, LoginData As Worksheet
Set LoginData = ThisWorkbook.Worksheets("MySheet")
UserName = LoginData.Cells(1, "K").Value
Password = LoginData.Cells(2, "K").Value

Dim iex As Object
Set iex = CreateObject("InternetExplorer.Application")

With iex

    .Navigate Url
    ieBusy iex
    .Visible = True
    Dim oLogin As Object, oPassword As Object
    iex.Document.querySelector(".username [id='p_lt_ctl01_LogonFormIndice_loginElem_UserName']").Focus
    Set oLogin = iex.Document.querySelector(".username [id='p_lt_ctl01_LogonFormIndice_loginElem_UserName']").Value = ""
    Set oPassword = iex.Document.querySelector(".password [type=password]").Value = ""

    oLogin.Value = UserName
    oPassword.Value = Password
    iex.Document.getElementById("submit-button").Click

End With

它询问关于一个对象

iex.Document.querySelector("id='p_lt_ctl01_LogonFormIndice_loginElem_UserName']").Focus

标签: htmlexcelvbaweb-scraping

解决方案


我对你的实际问题在哪里感到困惑。

提交有一个id

ie.document.getElementById("submit-button").click   ' 0r .submit

对于用户名:

ie.document.querySelector("[id='p_lt_ctl01_LogonFormIndice_loginElem_UserName']").value = ""

对于密码

ie.document.querySelector("[type=password]").value = ""

输入值时,有时.Focus在分配.value.

Public Sub GetInfo()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .navigate URL

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document.querySelector("[id='p_lt_ctl01_LogonFormIndice_loginElem_UserName']")
            .Focus
            .value = ""
        End With
        With .document.querySelector("[type=password]")
            .Focus 
            .value = ""
        End With
        .document.getElementById("submit-button").click

        While .Busy Or .readyState < 4: DoEvents: Wend
        Stop
        'Quit 
    End With

End Sub

推荐阅读