Work With List Box QTP

Posted by rajivkumarnandvani on May 3, 2009

When we use list box Object in QTP. QTP perform following operation on listbox . like

Browser(“Browser”).Page(“Page”)..WebList(“ListBox”).Select “ListBoxValue”

It select the value from List Box that value must be present in list box otherwise QTP give an error like that

“Cannot identify the specified item of the ListBox object. Confirm that the specified item is included in the object’s item collection.”

So before selection any value in list box Always insure the value you are going to select in list box must be present by use this method/Function

dim objListBox, sValuematch ,bTrue
set objListBox = Browser(“Browser”).Page(“Page”)..WebList(“ListBox”)
sValuematch =”Type Value that need to be check”
bTrue = fn_Valuematch_for_weblistbx(objListBox ,sValuematch )
if bTrue =True then
Browser(“Browser”).Page(“Page”)..WebList(“ListBox”).Select “ListBoxValue”
end if

REM ” this function Check the value u define match With in WEBListbox or Not if match return true otherwise False
Public Function fn_Valuematch_for_weblistbx(byval m_listboxobject, byval m_valuematch)
”check is m_listboxobject object or not
If IsObject(m_listboxobject ) = true and IsNull(m_valuematch) = false Then
Dim m_Getallfield,m_arAllfield,m_val,m_actual
m_arAllfield = m_listboxobject.GetROProperty(“all items”)
m_arAllfield =Split (m_arAllfield,”;”)
For each element in m_arAllfield
If m_valuematch = element then ‘ compare the text
fn_Valuematch_for_weblistbx=true ” if value match then return true & exit thr loop
exit for
fn_Valuematch_for_weblistbx = false
end if
print “Error wrong Parameter for function fn_Valuematch_for_listbx”,”Check paramenter value”
End If
End Function

21 Responses to “Work With List Box QTP”

  1. Albert Gareev said

    There is situation when with the example you presented QTP will still be throwing error message and stopping the execution: if the browser window is disabled by a pop-up dialog. In this case the item may exist by the selection is unavailable.

    The following code allows handling all types of errors.

    On Error Resume Next
    objWebList.Select sItem
    If Err.Number 0 Then
    ‘put your error handling here
    End If

    Checking every single item in the list is very useful when you do selection by RegEx i.e. you don’t know how exactly the item is displayed.

    sContent = objWebList.GetROProperty(“all items”)
    dvContent = Split(sContent, “;”)

    For iter = 0 To UBound(dvContent)

    ‘ If you want to ignore case – define boolMatchCase flag as FALSE
    If Not boolMatchCase Then
    sItem = LCase(dvContent(iter))
    sItem = dvContent(iter)
    End If

    ‘ Regex_Test is a simple function based on the RegExp object. Returns TRUE if the second argument matches the first one
    If Regex_Test(sItem, sValue) Then
    intItemIndex = iter
    sItemValue = dvContent(iter)
    ListFindItem = TRUE
    Exit Function
    End If


    ListFindItem = FALSE

  2. Thanks for the comment.
    I write the just simple way to work with list box not with error handling in proper way there will be many cases I did not take care of them just mention the basic which we generally do mistake in our script
    Your comments really very good

  3. Priya said

    I need a code to select random value from the list. I don’t need to compare with any value.
    What changes I need to make for the following code?
    Dim intNumItems, intRandomNumberIndex, strRandomValue
    intNumItems = Browser(“webpage”).Page(“page1”).WebList(“typeString”).GetROProperty(“item counts”)
    intRandomNumberIndex = Int(intNumItems * Rnd + 1)
    strRandomValue = Browser(“webpage”).Page(“page1”).WebList(“typeString”).GetItem(“intRandomNumberIndex”)
    Browser(“webpage”).Page(“page1”).WebList(“typeString”).Select “strRandomValue”

  4. Hi Priya,

    just you did little mistake rest u did well 😉

    see your last line you using variable strRandomValue as a string just change this thats it only.

    Browser(“webpage”).Page(“page1″).WebList(“typeString”).Select “strRandomValue” changed to

    Browser(“webpage”).Page(“page1″).WebList(“typeString”).Select strRandomValue

  5. Priya said

    Oh so silly.

    Thank you

  6. Most Welcome 🙂

  7. Priya said

    sorry another one.

    i have a select date pop-up box. Now each iteration should select future date and time. How to do this?
    Below script is original by recording. extend_2 is image link (like a calendar image which on clicking opens date/time popup box)

    On this pop-up, need to select time from drop down list and then click on the date link.

    Browser(“webpage1”).Page(“page1”).WebList(“time”).Select “4:20 pm”

    Appreciate your help

  8. From date popup you can not select with particular date wise with QTP. Is that Text Field where you can enter the date by type? if that so then you can use date function like

    Browser(“webpage1″).Page(“page1″).Webedit().set date
    see date function in qtp help file

  9. Priya said

    No that’s the thing. Cannot type manually in text field.

    Anyways thank you

  10. Hi can u send me web page of that application save webpage complete (file menu –>save page as) in Zip along with snap shot of that. let me see what else you can do becoz it seems popup is also web element

  11. Arun Kumar C said

    Hello Guys ,

    How could choose value from a list box through Datadriven approach.

    Assume i am reading value from Excel … ( Ex : Consider list box in Mercury tours demo website ) ..

  12. Hi Arun,

    just go through the help file of QTP Find parametrization, there you will get the solution. 😉

  13. Anonymous said

    hi this is srikanth ,i want to select listbox value through datadriventesting using excelsheet
    Eg:i want to select “M” column value,in listbox that value available in the application,how to select
    how to select

  14. getvaluefromsheet = DataTable.Value(“Column_Name”, dtGlobalSheet)

    Browser(“Browser”).Page(“Page”)..WebList(“ListBox”).Select getvaluefromsheet

  15. Saurabh said

    Hi Saurabh here…

    in ur above reply to arun….what if theres a case of web grid ?

    if we have to select from a web grid ?

  16. Hi Saurabh,

    Refer this

  17. ashutosh said

    i use same code as above but it give error on four line “more then on eargument
    Dim intNumItems, intRandomNumberIndex, strRandomValue

    intNumItems=Browser(“Title:=PAWZ.*”).Page(“title:=PAWZ.*”).Frame(“name:=body”).WebList(“name:=fldOS”).GetROProperty(“items count”)
    intRandomNumberIndex = Int(intNumItems * Rnd + 1)


    Browser(“Title:=PAWZ.*”).Page(“title:=PAWZ.*”).Frame(“name:=body”).WebList(“name:=fldOS”). Select strRandomValue

  18. strRandomValue=Browser(“Title:=PAWZ.*”).Page(“title:=PAWZ.*”).Frame(“name:=body”).WebList(“name:=fldOS”).GetItem(RandomNumberIndex)

    you are using your variable RandomNumberIndex as a text in GetItem method by using double quote remove the double Quote


  19. Hanaan said

    given a situation where there are three or more items in the web list i want qtp to select each item one by one and perform an action in my case to select the the item then click on a link at this point i want the coding to look for a value if found then ok else it needs to loop with every item on the list until either the value is found or there are no more items on the list to select

  20. Ch said


    I am getting error: “cannot identify object value..”

    myyear=Datepart(“yyyy”,Now()) -1
    Browser(“EASE”).Page(“EASE”).WebList(“name:=select”,”Index:=0″).Select “Oct”
    Browser(“EASE”).Page(“EASE”).WebList(“name:=select”,”Index:=1″).Select myyear

  21. Anonymous said

    Unable to select the list items present under webtables ( ex : Frame–>Webtable1–>webtable2–>webtable3–>SAPList( items)) here I am able to view the webtables while object spying and if we add the SAPList the SAPList will add directly to the Frame.

    Note : this is SAP web application.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: