gpt4 book ai didi

excel - 检查用户是否已经登录,如果是则跳过

转载 作者:行者123 更新时间:2023-12-04 21:29:55 24 4
gpt4 key购买 nike

我正在创建网页抓取代码以登录网站并输入一些数据。在进入实际页面之前有登录表单。我的代码首先登录。但是,如果您已经登录,网站不会要求登录。有没有办法检查是否需要登录?

这是我的代码:

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

关于excel - 检查用户是否已经登录,如果是则跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56328773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com