Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 检查用户是否已登录,如果是,则跳过_Excel_Vba_Web Scraping - Fatal编程技术网

Excel 检查用户是否已登录,如果是,则跳过

Excel 检查用户是否已登录,如果是,则跳过,excel,vba,web-scraping,Excel,Vba,Web Scraping,我正在创建网页抓取代码登录到网站,并输入一些数据。在进入实际页面之前有一个登录表单。我的代码是先登录的。然而,若你们已经登录,网站不会要求你们登录。有没有办法检查是否需要登录 这是我的密码: Sub ChechAutomate() Dim ie As New InternetExplorer, url As String, ws As Worksheet Set ws = ThisWorkbook.Sheets("Other Data") url = "https:/

我正在创建网页抓取代码登录到网站,并输入一些数据。在进入实际页面之前有一个登录表单。我的代码是先登录的。然而,若你们已经登录,网站不会要求你们登录。有没有办法检查是否需要登录

这是我的密码:

Sub ChechAutomate()
    Dim ie As New InternetExplorer, url As String, ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Other Data")

    url = "https://infra.com/"

    With ie
        .Visible = True
        .Navigate2 url

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

        With .Document
            .querySelector("[name=userName]").Value = "username"
            .querySelector("[name=password]").Value = "password123"
            .querySelector("[type=submit]").Click

            'While .Busy Or .ReadyState < 4: DoEvents: Wend

            .querySelector("[id=companySearchKeyData]").Value = ws.Range("T24").Value
            .querySelector("[type=submit]").Click
        End With

    End With

End Sub

您可以创建一个条件语句来检查您所在的页面。在这种情况下,以下逻辑可能会有所帮助:

Sub ChechAutomate()
    Dim IE As New InternetExplorer, url As String, ws As Worksheet
    Dim Html As HTMLDocument, idCheck As Object

    Set ws = ThisWorkbook.Sheets("Other Data")

    url = "https://infra.com/"

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

    Set idCheck = Html.querySelector("#login-bis-id-btn")
    If Not idCheck Is Nothing Then
        Html.querySelector("[name=userName]").Value = "username"
        Html.querySelector("[name=password]").Value = "password123"
        Html.querySelector("[type=submit]").Click
        While IE.Busy Or IE.readyState < 4: DoEvents: Wend
    Else:
        Debug.Print "You are already logged-in"
    End If

    'I don't know the role of the following two lines, though

    Html.querySelector("[id=companySearchKeyData]").Value = ws.Range("T24").Value
    Html.querySelector("[type=submit]").Click
End Sub

您可以创建一个条件语句来检查您所在的页面。在这种情况下,以下逻辑可能会有所帮助:

Sub ChechAutomate()
    Dim IE As New InternetExplorer, url As String, ws As Worksheet
    Dim Html As HTMLDocument, idCheck As Object

    Set ws = ThisWorkbook.Sheets("Other Data")

    url = "https://infra.com/"

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

    Set idCheck = Html.querySelector("#login-bis-id-btn")
    If Not idCheck Is Nothing Then
        Html.querySelector("[name=userName]").Value = "username"
        Html.querySelector("[name=password]").Value = "password123"
        Html.querySelector("[type=submit]").Click
        While IE.Busy Or IE.readyState < 4: DoEvents: Wend
    Else:
        Debug.Print "You are already logged-in"
    End If

    'I don't know the role of the following two lines, though

    Html.querySelector("[id=companySearchKeyData]").Value = ws.Range("T24").Value
    Html.querySelector("[type=submit]").Click
End Sub
最简单的检查是使用querySelectorAll返回节点列表,并在With.document中检查其长度

最简单的检查是使用querySelectorAll返回节点列表,并在With.document中检查其长度


这取决于网站,但我知道很多web应用程序的右上角都有一段文字,上面写着“登录”或类似的内容,但如果你登录了,上面会写着与我的帐户不同的内容。你可以先刮一下那个区域,看看是否需要登录。登录页面上有id=login bis id btn,其他页面上没有。这取决于网站,但我知道很多web应用程序右上角有一点文字,上面写着登录或类似的内容,但是如果你登录了,它会显示一些不同的信息,比如我的账户。你可以先刮一刮那个区域,看看是否需要登录。登录页面上有id=login bis id btn,其他页面上没有。这也是框显的好方法,也是框显的好方法+