Share
Go down
gkpandi
Active particpant
Active particpant
Posts : 14
Join date : 2013-05-23

How to select an item from the Drop Down combo thru' selenium web driver

on Mon Jun 03, 2013 6:52 pm
Hi Friends,

I'm using Selenium WebDriver 2.0 and I need a script for selecting an item from the drop down combo box.

I got the (x)path information when I recorded my webpage using Selenium IDE and my code is the following:

driver.findElement(By.xpath("//div[@id='j_idt51:country']/div[2]/span")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[@id='j_idt51:country_panel']/ul/li[2]")).click();

When I run this code from Eclipse, system is not selecting the SECOND element from the drop down combo instead it is retaining the first element. See this screen shot where I'm unable to select USA item from the combo box thru' the above script.



Please guide how can I solve this problem?

Thanks in advance
tarun3kumar
Master
Master
Posts : 186
Join date : 2012-02-14
http://seleniumtests.com

Re: How to select an item from the Drop Down combo thru' selenium web driver

on Tue Jun 04, 2013 10:52 am

_________________
Thanks
Tarun K
Free Selenium Training
gkpandi
Active particpant
Active particpant
Posts : 14
Join date : 2013-05-23

Re: How to select an item from the Drop Down combo thru' selenium web driver

on Tue Jun 04, 2013 11:25 am
I tried with 'Select' class like this.

Select sel = new Select (driver.findElement(By.xpath("//div[@id='j_idt51:country_panel']/ul/li[2]")));
sel.click();


But it is not working instead it is displaying the following error message in the console.

[ Element should have been select but was span ]
tarun3kumar
Master
Master
Posts : 186
Join date : 2012-02-14
http://seleniumtests.com

Re: How to select an item from the Drop Down combo thru' selenium web driver

on Wed Jun 05, 2013 1:20 pm
It would work if element has "select" tag

_________________
Thanks
Tarun K
Free Selenium Training
mail2prassad
Amateur
Amateur
Posts : 41
Join date : 2013-06-05

Re: How to select an item from the Drop Down combo thru' selenium web driver

on Wed Jun 05, 2013 11:12 pm
Does driver.findElement(By.xpath("//div[@id='j_idt51:country_panel']/ul/li[2]")) refer to the correct element.
Try to print / debug
driver.findElement(By.xpath("//div[@id='j_idt51:country_panel']/ul/li[2]")).getAttribute("innerHTML")';
and see whether it got the required element in it.
Sponsored content

Re: How to select an item from the Drop Down combo thru' selenium web driver

Back to top
Permissions in this forum:
You cannot reply to topics in this forum