get the count of the items in the list box

2,013 views
Skip to first unread message

sam6

unread,
Jun 20, 2011, 5:01:52 AM6/20/11
to Selenium Users
Hi,

I am newbie to selenium.
I have a select list box with few items.
I need to get the count of items in the list box and access each of
them.

can you please let me know on how to get this
thanks
sam

AV

unread,
Jun 20, 2011, 5:36:40 AM6/20/11
to Selenium Users
Try GetXpathCount(). Just provide the common Xpath correctly and you
should be able to count them.

For accessing them you can use a For() loop and access each one of
them.

Thanks
Abhinav

tulsi.tester

unread,
Jun 20, 2011, 9:59:56 AM6/20/11
to Selenium Users
Hi sam,

to get the count of the items in the select list box we have to use
Xpath.

see the following example

Xpath = "//div[@class='one']/select/option" ---->
this is the xpath of a list box

Pass this xpath to getXpathCount() like as

Number count = getXpathCount(Xpath);
int listCount = count.intValue(); ---> this gives
the value of the list items in the select box

If any problem revert back

sam6

unread,
Jun 21, 2011, 1:19:41 AM6/21/11
to Selenium Users
thanks..

this answers my first question.
I also need to access the each item available in the list.
how can i do that?

thanks
Sam6

On Jun 20, 6:59 pm, "tulsi.tester" <tulsi.tes...@gmail.com> wrote:
> Hi sam,
>
> to get the count of the items in the select list box we have to use
> Xpath.
>
> see the following example
>
> Xpath = "//div[@class='one']/select/option"                     ---->
> this is the xpath of a list box
>
> Pass this xpath to getXpathCount()  like as
>
> Number count = getXpathCount(Xpath);
> int listCount = count.intValue();                   ---> this gives
> the value of the list items in the select box
>
> If any problem revert back
>

Sunil Kumar Darshanam

unread,
Jun 21, 2011, 2:10:36 AM6/21/11
to seleniu...@googlegroups.com
If your listbox/dropdown is identified by the below xpath
WebElement elemet = driver.findElement(By.xpath("//select[@id='destination']"));

You can add "option" to the xpath with its value to access the item in the list.
WebElement wel =
driver.findElement(By.xpath("//select[@id='dropdown_id']/option[@value='BillPay']"));

> --
> You received this message because you are subscribed to the Google Groups "Selenium Users" group.
> To post to this group, send email to seleniu...@googlegroups.com.
> To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.
>
>

Sunil Kumar Darshanam

unread,
Jun 21, 2011, 2:12:32 AM6/21/11
to seleniu...@googlegroups.com
You also have "Select" classs in
"org.openqa.selenium.support.ui.Select" (If you are using webdriver
2.0 rc2).

Select sel = new
Select(driver.findElement(By.xpath("//select[@id='destination']")));
sel.selectByVisibleText("Bill pay");

You have some more methods like getOptions to get all the list of
items from listitem.

sam6

unread,
Jun 21, 2011, 2:43:53 AM6/21/11
to Selenium Users
I am using selenium 1.0.

I tried the xpathcount to get the count of items in the list..but it
returned 1 though it has several items.
below is the code:

System.out.println("notAudited:"+_browser.getXpathCount("//
select[@name='notAudited']"));

pls correct me if this is wrong
Also, i need to access each option in the select list.pls share a
sample


thanks
sam6

On Jun 21, 11:12 am, Sunil Kumar Darshanam <sdarsha...@gmail.com>
wrote:
> You also have "Select" classs in
> "org.openqa.selenium.support.ui.Select" (If you are using webdriver
> 2.0 rc2).
>
> Select sel = new
> Select(driver.findElement(By.xpath("//select[@id='destination']")));
> sel.selectByVisibleText("Bill pay");
>
> You have some more methods like getOptions to get all the list of
> items from listitem.
>
> On Tue, Jun 21, 2011 at 11:40 AM, Sunil Kumar Darshanam
>
> <sdarsha...@gmail.com> wrote:
> > If your listbox/dropdown is identified by the below xpath
> > WebElement elemet = driver.findElement(By.xpath("//select[@id='destination']"));
>
> > You can add "option" to the xpath with its value to access the item in the list.
> > WebElement wel =
> > driver.findElement(By.xpath("//select[@id='dropdown_id']/option[@value='BillPay']"));
>

Mark Collin

unread,
Jun 21, 2011, 9:05:55 AM6/21/11
to seleniu...@googlegroups.com
Try:

System.out.println("notAudited:"+_browser.getXpathCount("//select[@name='not

Audited']/option"));


thanks
sam6


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

tulsi.tester

unread,
Jun 21, 2011, 1:51:07 PM6/21/11
to Selenium Users
Hi Sam,

Keep that Xpath in for loop so that you can access all the items

for( int i = 0; i <= listCount; i++)
{
String text = selenium.getText("//div[@class='one']/select/
option["+ i +"]");
Reporter.log("List item is " +text);
}

Please revert me if not works.
Reply all
Reply to author
Forward
0 new messages