首页 > 解决方案 > 跳过登录页面,如果已经登录

问题描述

如果 Internet Explorer 首次使用代码启动,则没有问题。

如果网站已经登录系统,我会得到一个错误

对象不支持...

如果网页已经登录,我想跳过这个登录阶段。

我使用的示例代码

Sub Login()
    Dim IE As New InternetExplorer, url As String
    Dim Html As HTMLDocument, idCheck As Object

    url = "https://abc-site.com/"

    With IE
        .Visible = True
        .Navigate2 url
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document
    End With

    Set idCheck = Html.querySelector(".Log In")
    If Not idCheck Is Nothing Then
        Html.querySelector("[name=userName]").Value = "username"
        Html.querySelector("[name=password]").Value = "password"
        Html.querySelector("[type=submit]").Click
        While IE.Busy Or IE.readyState < 4: DoEvents: Wend
    Else
    End If
End Sub

USER NAME
<input id="Content1_UserIDView_txtUserid_UiInput" ng-class="{'error' : AuthMethod.error}" type="text" class="form-control bb-input bb-username-input ng-pristine ng-scope ng-empty ng-invalid ng-invalid-required ng-touched" placeholder="User ID" autocomplete="off" ng-model="AuthMethod.user" ng-change="AuthMethod.getMethodOnChange(method.input.id)" ng-disabled="false" ng-if="method.label.text !== ' *' &amp;&amp; method.input.id.length > 0" ng-init="AuthMethod.getUser()" tabindex="0" focus-if="" required="" aria-invalid="true">

PASSWORD
<input id="Content_UserIDView_tbxPassword_UiInput" ng-class="{'error' : AuthMethod.error}" type="password" class="form-control bb-input bb-password-input ng-pristine ng-scope ng-empty ng-invalid ng-invalid-required ng-touched" placeholder="Password" autocomplete="off" ng-model="AuthMethod.pass" ng-change="AuthMethod.getPassOnChange(method.input.id, $event)" ng-disabled="false" ng-if="method.label.text !== ' *' &amp;&amp; method.input.id.length > 0" ng-init="AuthMethod.getPass()" tabindex="0" focus-if="AuthMethod.authMethodId[0].label.text.length === 0" required="" aria-invalid="true">

Login Button
<a href="#" class="btn-primary bb-binding" ng-disabled="UserIdButton.shouldDisableButton()" ng-click="!UserIdButton.shouldDisableButton() &amp;&amp; UserIdButton.click(UserIdButton.userIdViewBtn.button.id)" title=" Log In" disabled="disabled">
                 Log In
        </a>

标签: htmlvbainternet-explorerweb-scraping

解决方案


从链接中找到上述查询的解决方案

If .querySelectorAll("#Content1_UserIDView_txtUserid_UiInput").length > 0 Then
    'do login page stuff
Else
    'Skip login code
End If
    'proceed with the other code....

感谢 QHarr 它帮助了我并解决了我的问题。


推荐阅读