How to select random list item (QTP, VBScript)

Posted by Albert Gareev on Nov 24, 2011 | Categories: How toSource codeWeb


Get object reference. Get total number of items in the list. Generate an index within the given range. Select item by index.


Dim objPage, objList
Dim boolRC
Dim intItemsCount, intRndItem

' "Google Advanced Search" page is up
' "Results per page" WebList exists and enabled

'Get page object
Set objPage = Browser("CreationTime:=0").Page("title:=Google Advanced Search")
boolRC = objPage.Exist(0)
If Not boolRC Then
End If

'Get list object
Set objList = objPage.WebList("name:=num")
boolRC = objList.Exist(0)
If Not boolRC Then
End If

'Get number of list items
intItemsCount = objList.GetROProperty("items count")
'Create random index
intRndItem = CInt(Rnd*intItemsCount)
'Select the item
'Note: selection by index is zero-based
objList.Select "#" & intRndItem

  • Leave a Reply

    * Required
    ** Your Email is never shared

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.