Monday, December 2, 2013

Selenium:Reading Existing list of values from ListBox Using WebDriver

Reading Existing list of values from ListBox Using WebDriver

import java.util.List;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;

// - Reading Existing list of values from ListBox
public class WebDriver18 {
/**
* @param args
*/
public static WebDriver oBrowser;
public static String sUrl = "http://www.goibibo.com";
public static void main(String[] args)
{
boolean bIsBrowserOpened;
bIsBrowserOpened = OpenBrowser();
if (bIsBrowserOpened)
{
Get_ListBox_Items();
CloseBrowser();
}
}
public static boolean OpenBrowser()
{
try
{
oBrowser = new FirefoxDriver();
oBrowser.get(sUrl);
try
{
Thread.sleep(5000L);
}
catch (Exception e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
return false;
}
return true;
}

public static void CloseBrowser()
{
oBrowser.close();
}
public static void Get_ListBox_Items()
{
WebElement oListBox;
int iElement, iCount;
List<WebElement> oAllListValues;
oListBox = oBrowser.findElement(By.id("gi_source"));
oAllListValues = oListBox.findElements(By.tagName("option"));
iCount = oAllListValues.size();
System.out.println("No of Items found in list box ="+String.valueOf(iCount));
System.out.println("*********************************");
for (iElement=0; iElement<iCount; iElement++)
{
System.out.printf("\n Item (%d/%d) = %s",
iElement+1,iCount,
oAllListValues.get(iElement).getText());
}

}

}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...