VBA 将 ie 元素传递给函数
VBA pass ie element to a function
由于我在可靠地检查 ie.busy 和 ie.readystate 以检查网页是否已加载时遇到困难,我有一个函数可以检查是否存在特定元素以确认页面已加载。
下面的代码有效,但很笨拙,因为我必须为我调用的每个要检查的页面设置一个单独的函数。
理想情况下,我想将行 "ie.document.getElementsByClassName("container centered-form single-column clearfix")(0).innerText" 作为参数传递给函数,以便该函数可用于任何网页,但我不能弄清楚如何做到这一点。
非常感谢
Function checkLoginUrl(ie As Variant, strMessageContent As String, compareString As String) As Boolean
EndTime = Now + TimeValue("00:00:22")
Application.Wait (Now + TimeValue("00:00:01")) 'wait a bit to start with as code falls here as too quick
'get the value of the element we are monitoring
dd = ie.document.getElementsByClassName("container centered-form single-column clearfix")(0).innerText
Do While dd = ""
DoEvents
'recheck the value we are monitoring
dd = ie.document.getElementsByClassName("container centered-form single-column clearfix")(0).innerText
Printlog (Now & " - " & dd & " - and waited 3 secs")
Application.Wait (Now + TimeValue("00:00:03"))
If StartTime > EndTime Then
Printlog ("Tried to log on for 22 seconds without success")
Exit Do
End If
Loop
'so dd should be populated now so check
If dd Like "*" & compareString & "*" Then
Printlog (strMessageContent & " loaded successfully")
retval = True
Else
Printlog (strMessageContent & " failed to load")
retval = False
End If
launchUrl = retval
End Function
您是否可以将 ie.document.getElementsByClassName("container centered-form single-column clearfix")(0).innerText 的值存储在变量中并将字符串参数传递给您的通过在函数中添加一个字符串参数来实现功能,如下所示?
Function checkLoginUrl(ie As Variant, strMessageContent As String, compareString As String, element as string) As Boolean
如果我对您的上述描述有任何误解,请告诉我。我会努力改正自己的。
也许熟悉一下 css selectors,您可以在其中指定模式以将节点与字符串匹配,然后将其传递给函数。简单一点的功能为好,并确保您使用的是一致的命名方式。目前,由于结束赋值变量具有不同的名称,您不会从函数返回。为了获得更大的灵活性,还将等待时间作为参数传递。
直接的优点是您无需担心所有不同的链接方法 (getElementsBy...),并且您使用的是现代浏览器优化的方法。
Option Explicit
Public Sub test()
Dim ie As New InternetExplorer, url As String, result As Boolean
Dim strMessageContent As String, compareString As String
Const MAX_WAIT_SEC As Long = 10
url = "https://example.com"
With ie
.Visible = True
.Navigate2 "url"
While .Busy Or .readyState < 4: DoEvents: Wend
cssSelector = ".container.centered-form.single-column.clearfix"
result = checkLoginUrl(ie, cssSelector, strMessageContent, compareString, MAX_WAIT_SEC) 'you might consider renaming 'result' as 'found' _
and move the final If statement out the function and have it following here ......
Stop
.Quit
End With
End Sub
Public Function checkLoginUrl(ByVal ie As Object, ByVal cssSelector As String, ByVal strMessageContent As String, compareString As String, ByVal MAX_WAIT_SEC As Long) As Boolean
Dim t As Date, ele As Object, dd As String, found As Boolean
t = Timer
Do
On Error Resume Next
Set ele = ie.document.querySelector(cssSelector)
dd = ele.innerText
found = InStr(dd, compareString) > 0
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop Until found
If found Then
Printlog strMessageContent & " loaded successfully"
Else
Printlog "Tried to log on for " & MAX_WAIT_SEC & " seconds without success"
Printlog strMessageContent & " failed to load"
End If
checkLoginUrl = found
End Function
由于我在可靠地检查 ie.busy 和 ie.readystate 以检查网页是否已加载时遇到困难,我有一个函数可以检查是否存在特定元素以确认页面已加载。
下面的代码有效,但很笨拙,因为我必须为我调用的每个要检查的页面设置一个单独的函数。 理想情况下,我想将行 "ie.document.getElementsByClassName("container centered-form single-column clearfix")(0).innerText" 作为参数传递给函数,以便该函数可用于任何网页,但我不能弄清楚如何做到这一点。
非常感谢
Function checkLoginUrl(ie As Variant, strMessageContent As String, compareString As String) As Boolean
EndTime = Now + TimeValue("00:00:22")
Application.Wait (Now + TimeValue("00:00:01")) 'wait a bit to start with as code falls here as too quick
'get the value of the element we are monitoring
dd = ie.document.getElementsByClassName("container centered-form single-column clearfix")(0).innerText
Do While dd = ""
DoEvents
'recheck the value we are monitoring
dd = ie.document.getElementsByClassName("container centered-form single-column clearfix")(0).innerText
Printlog (Now & " - " & dd & " - and waited 3 secs")
Application.Wait (Now + TimeValue("00:00:03"))
If StartTime > EndTime Then
Printlog ("Tried to log on for 22 seconds without success")
Exit Do
End If
Loop
'so dd should be populated now so check
If dd Like "*" & compareString & "*" Then
Printlog (strMessageContent & " loaded successfully")
retval = True
Else
Printlog (strMessageContent & " failed to load")
retval = False
End If
launchUrl = retval
End Function
您是否可以将 ie.document.getElementsByClassName("container centered-form single-column clearfix")(0).innerText 的值存储在变量中并将字符串参数传递给您的通过在函数中添加一个字符串参数来实现功能,如下所示?
Function checkLoginUrl(ie As Variant, strMessageContent As String, compareString As String, element as string) As Boolean
如果我对您的上述描述有任何误解,请告诉我。我会努力改正自己的。
也许熟悉一下 css selectors,您可以在其中指定模式以将节点与字符串匹配,然后将其传递给函数。简单一点的功能为好,并确保您使用的是一致的命名方式。目前,由于结束赋值变量具有不同的名称,您不会从函数返回。为了获得更大的灵活性,还将等待时间作为参数传递。
直接的优点是您无需担心所有不同的链接方法 (getElementsBy...),并且您使用的是现代浏览器优化的方法。
Option Explicit
Public Sub test()
Dim ie As New InternetExplorer, url As String, result As Boolean
Dim strMessageContent As String, compareString As String
Const MAX_WAIT_SEC As Long = 10
url = "https://example.com"
With ie
.Visible = True
.Navigate2 "url"
While .Busy Or .readyState < 4: DoEvents: Wend
cssSelector = ".container.centered-form.single-column.clearfix"
result = checkLoginUrl(ie, cssSelector, strMessageContent, compareString, MAX_WAIT_SEC) 'you might consider renaming 'result' as 'found' _
and move the final If statement out the function and have it following here ......
Stop
.Quit
End With
End Sub
Public Function checkLoginUrl(ByVal ie As Object, ByVal cssSelector As String, ByVal strMessageContent As String, compareString As String, ByVal MAX_WAIT_SEC As Long) As Boolean
Dim t As Date, ele As Object, dd As String, found As Boolean
t = Timer
Do
On Error Resume Next
Set ele = ie.document.querySelector(cssSelector)
dd = ele.innerText
found = InStr(dd, compareString) > 0
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop Until found
If found Then
Printlog strMessageContent & " loaded successfully"
Else
Printlog "Tried to log on for " & MAX_WAIT_SEC & " seconds without success"
Printlog strMessageContent & " failed to load"
End If
checkLoginUrl = found
End Function