我有以下代码从两个需要登录的页面中提取表格。代码打开IE,进入登录页面,输入凭据,然后拉出2个表。
但是,如果IE已经与用户一起登录,它会直接将您带到此页面(因此代码错误,因为没有登录字段): https://www.example.com/taskprocessing/manage.jsp
我需要添加一个IF语句,如果它出现在此页面上,单击此链接注销,然后继续使用凭据登录: https://www.example.com/taskprocessing/logout.jsp
Sub GetTable()
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate "https://www.example.com/taskprocessing/login.jsp"
Do Until .ReadyState = 4
DoEvents
Loop
.Document.all.Item("Username").Value = "username123"
.Document.all.Item("Password").Value = "password123"
.Document.forms(0).submit
End With
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;https://www.example.com/taskprocessing/report_pending_tasks.jsp", Destination:=Range("J1"))
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;https://www.example.com/taskprocessing/report_task_processing_stats.jsp", Destination:=Range("A1"))
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = """user"""
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End With
End Sub
答案 0 :(得分:0)
如果不需要,您可以跳过登录过程。为此,您可以按如下方式重新构建顶级代码:
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate "https://www.example.com/taskprocessing/login.jsp"
Do Until .ReadyState = 4
DoEvents
Loop
On Error Resume Next 'this will help to understand if there was an error when trying retrieving the UserName field
Set userField = .Document.getElementById("Username")
If Err.Number = 0 Then 'if there was no error when trying to search for the username...
.Document.all.Item("Username").Value = "username123"
.Document.all.Item("Password").Value = "password123"
.Document.forms(0).submit
End If
On Error GoTo 0 'turn-off the error handling once again
End With
我添加的行是:
On Error Resume Next
...如果在查找不存在的元素时出现错误,请避免代码中断;
Set userField = .Document.getElementById("Username")
...查看页面是否已登录或注销。如果它已注销,则执行此操作时不会出现错误。如果不是,那么这将导致“对象不存在错误”。
If Err.Number = 0 Then
...所以如果在这样做时没有错误,我们继续登录...否则我们就继续做我们正在做的事情。
On Error GoTo 0
...最后,我们关闭错误处理,如果页面中有任何其他错误,您将继续定期收到通知。
答案 1 :(得分:0)
谢谢!我需要它注销,所以我添加了一个Else声明。
On Error Resume Next 'this will help to understand if there was an error when trying retrieving the UserName field
Set userField = .Document.getElementById("Username")
If Err.Number = 0 Then 'if there was no error when trying to search for the username...
.Document.all.Item("Username").Value = "username123"
.Document.all.Item("Password").Value = "password123"
.Document.forms(0).submit
Else
.Navigate "https://www.example.com/taskprocessing/logout.jsp"
Do While ie.Busy: DoEvents: Loop
Do Until .ReadyState = 4
DoEvents
Loop
.Document.all.Item("Username").Value = "username123"
.Document.all.Item("Password").Value = "password123"
.Document.forms(0).submit
End If
On Error GoTo 0 'turn-off the error handling once again