Monday, February 18, 2013

Selenium : Ways to Identify objects

The Following Example use Parent and Child object concept to capture a particular object.Though the objective can be realised much simpler i have followed this method for the understanding of the concept .

//Example of Google homepage

/*Class to detect Google Search button ,using ParentObject ,
* findElement and findElements concepts
*/
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 Test
{
public
static void main(String[] args) throws InterruptedException
{
WebDriver driver= new
FirefoxDriver();
EventFiringWebDriver d=new
EventFiringWebDriver(driver);
d.get("http://www.google.co.in/");
//WebElement box = d.findElement(By.xpath("//div[@id='als']"));
WebElement box = d.findElement(By.id("mngb"));
//List names =box.findElements(By.id("gbqfba"));
WebElement names =box.findElement(By.id("gbqfba"));
System.out
.println(names.getText());
//JOptionPane.showMessageDialog(null, "names.size()="+names.size());

box = d.findElement(By.className("jsb"));//id("mngb"));
names =box.findElement(By.className("gbqfba"));
System.out
.println(names.getText()); 

box = d.findElement(By.id("gsr"));//id("mngb"));
names =box.findElement(By.name("btnK"));//id("gbqfba"));
System.out
.println(names.getText());

box = d.findElement(By.xpath("//*[@id='body']/center/div[2]"));//id("mngb"));
names =box.findElement(By.xpath("//*[@id='gbqfba']"));//id("gbqfba"));
System.out
.println(names.getText());
 
d.quit();
}
}
 
 
 
 
 
 
 
 

No comments:

Post a Comment