如果有人能为此提出解决方案,我将非常感激。
我有一个简单的功能,希望在包含网络列表的网页上打开浏览器,该网页列表的每个值都代表一个帐户。选择帐户后,将显示其产品(如果有)。
功能目标是检索具有产品(第一个要找到的产品)的帐户索引,如果没有产品,则检索-1。
我无法弄清楚是什么导致它的问题是,当我调试它时,该函数将返回正确的结果 - 意味着使用F10逐步运行代码,但会返回错误的结果如果我会经常跑(F5)。这种行为是一致的,并且函数每次为每种类型的运行检索相同的结果,这意味着它不是一个让函数返回随机答案的错误。
这是功能:
' @return: a random account index with products if one exists
' otherwise returns -1
Public Function getRandomAccountWithProducts()
On Error Resume Next
Set Page1 = Browser("micclass:=browser").Page("micclass:=Page")
Set br = Browser("micclass:=Browser")
originalURL = br.GetROProperty("URL")
br.Navigate Environment.Value("SOME URL") & "REST OF URL"
br.Sync
Page1.WebList("name:=accountId").Select "#1"
br.Sync
' Display only products
Page1.WebRadioGroup("name:=name0").Click
Page1.WebList("name:=name1").Select "Display None"
Page1.WebList("name:=name2").Select "Display None"
Page1.WebButton("value:=Apply","visible:=True").Click
' Init
numOfAccounts = Page1.WebList("name:=accountId").GetROProperty("items count") - 1
If numOfAccounts < 1 Then
getRandomAccountWithProducts = -1
Reporter.ReportEvent micFail, "Number of accounts","There are no accounts. No account with products exists"
Exit Function
End If
hasProducts = false
accountIndex = 1
' Get account with products
While ((Not hasProducts) AND (accountIndex =< numOfAccounts))
' Return account if has products
If Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5) Then
hasProducts = true
End If
If (Not hasProducts) Then
accountIndex = accountIndex + 1
Page1.WebList("name:=accountId").Select "#" & accountIndex
End If
Wend
br.Navigate originalURL
Set Page1= Nothing
Set br = Nothing
' If no account has products, report and exit, else return selected account index
If Not hasProducts Then
Reporter.ReportEvent micFail,"Accounts","No account has products."
getRandomAccountWithProducts = -1
Else
getRandomAccountWithProducts = accountIndex
End If
If Err<>0 Then
errorMessage = "Error number: " & Err.Number & vbNewLine & "Error description: " & Err.Description & vbNewLine & "Error source: " & Err.Source
Reporter.ReportEvent micFail,"Run Time Error",errorMessage
Err.Clear
End If
On Error GoTo 0
End Function
我在奔腾4,3.2 GHZ,2 GB RAM,Win XP,SP 3,IE 7,QTP 10.0 Build 513上运行
谢谢!
答案 0 :(得分:0)
您是否考虑过使用所有商品属性?
AllItems = Page1.WebList("name:=accountId").GetROProperty("all items")
SplitItems = Split(AllItems, ";")
Found = False
For i = 0 To UBound(AllItems)
If AllItems(i) = "<product>" Then
Found = True
Exit For
End If
Next
答案 1 :(得分:0)
感谢Jonty,
找到了解决方案问题出在以下部分:
' Get account with products
While ((Not hasProducts) AND (accountIndex =< numOfAccounts))
' Return account if has products
If Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5) Then
hasProducts = true
End If
If (Not hasProducts) Then
accountIndex = accountIndex + 1
Page1.WebList("name:=accountId").Select "#" & accountIndex
End If
Wend
第一次进入循环时,该帐户确实没有任何产品,所以很明显没有人认出。因此,accountIndex增加了1,并在网络列表中选择了相应的帐户。
这不是问题所在。 select方法导致页面刷新和条件Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5)
因此在加载Web列表之前进行了评估,返回false。
我考虑过这个选项,但我认为(错误地显然)存在(5)应该做的伎俩,但它似乎与预期不同。
谢谢,
阿龙