Monday, December 2, 2013

Selenium:Getting count of Different Web Element displayed in the page using WebDriver

Getting count of Different Web Element displayed in the page using WebDriver

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

//Getting count of Different Web Element  displayed
public class WebDriverElementCount {

public static WebDriver oBrowser;
public static String sUrl="http://in.yahoo.com";
public static void main(String[] args)
{
boolean bIsBrowserOpened;

bIsBrowserOpened=OpenBrowser();
if(bIsBrowserOpened)
{

Display_Element_Count();
CloseBrowser();
}

}
public static boolean OpenBrowser()
{
try
{
oBrowser=new FirefoxDriver();
oBrowser.get(sUrl);
try
{
Thread.sleep(5000L);
}
catch(Exception e)
{
e.printStackTrace();
}
return true;
}
catch(Exception e)
{
System.err.println(e.getMessage());
return false;
}
}

public static void Display_Element_Count()
{
Get_Element_Count("Link","//a");
Get_Element_Count("Image","//img");
Get_Element_Count("TextBox","//input[@type='text']");
Get_Element_Count("Form Submit Button","//input[@type='submit' or @type='clear']");
Get_Element_Count("HTML 4/5 button","//button");
Get_Element_Count("Radio Button","//input[@type='radio']");
Get_Element_Count("CheckBox","//input[@type='checkbox']");
Get_Element_Count("List Box","//select");
Get_Element_Count("Activex Button","//object");
Get_Element_Count("Text Area","//textarea");
}
public static void Get_Element_Count( String sElementName,String sXpath_of_Element)
{
int iCount;
if(!sXpath_of_Element.isEmpty())
{
iCount=oBrowser.findElements(By.xpath(sXpath_of_Element)).size();
System.out.printf("\n No of (%s)  Element= %d" ,
sElementName,iCount);
}
}
public static void CloseBrowser()
{
oBrowser.close();
}

}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...