Monday, February 18, 2013

Selenium: To find all the buttons existing in "Googe.com" Homepage

The below code fetches string present inside all the buttons present inside "Google.com" Home page.

/* There is a peculiar scenarion I encountered while working this scenario, where the buttons actually present is 2 but we get totla of 3 buttons by running the code -- We figured out what the button was , hope u'll find too */

import
java.util.List;
import
javax.swing.JOptionPane;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.support.events.EventFiringWebDriver;
public
class Google
{
public static void main(String[] args) throws InterruptedException
{
String frnd;
WebDriver driver=
new FirefoxDriver();
EventFiringWebDriver d=
new EventFiringWebDriver(driver);
d.get(
"http://www.google.co.in/");
WebElement box = d.findElement(By.id(
"gsr"));//id("mngb"));
List names =box.findElements(By.tagName(
"button"));
JOptionPane.showMessageDialog(
null, "names.size()="+names.size());
for (int i=0 ; i<names.size();i++ )//(WebElement) names.get(i)).getText() != null ||
{
if (((WebElement) names.get(i)).getText().length()>2)
{
System.
out.println(((WebElement) names.get(i)).getText()+" "+i);
}
else if (((WebElement) names.get(i)).getText()== " ")
{
System.
out.println("---CONTAINS empty Space");
}
else if (((WebElement) names.get(i)).getText()==null)
{
System.
out.println("Contains null button");
}
else if (((WebElement) names.get(i)).getText().length()<=2)
{
System.
out.println(((WebElement) names.get(i)).getAttribute("id")+"---"+i);
}
}
d.quit();
}
}
 
 
//for (int i=0 ; i< names.size(); i++)
//{
// frnd=names.get(i).getText();
// //JOptionPane.showMessageDialog(null, "hi");
// System.out.println( ((WebElement) names.get(i)).getText());
//}
//JOptionPane.showMessageDialog(null, a);
//Thread.sleep(5000L);
 
 

No comments:

Post a Comment