Handling Elements in Selenium Part 2


Handling Elements in Selenium Part-2

i) Handle Browser
ii) Handle Edit box
iii) Handle Text Area, Error Message, Window Dialog
iv) handle Button
-----------------------
Return/Capture value from Edit box

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("India123");
System.out.println(driver.findElement(By.id("Email")).getAttribute("value"));
}
}
-----------------------
v) Handle Image

Three types of Image elements in Web Environment.

1) General Image (No functionality)

2) Image Button (Submits)

3) Image Link (Directs to another page/location)

Example:

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
System.out.println(driver.findElement(By.id("hplogo")).isDisplayed());//true
System.out.println(driver.findElement(By.id("hplogo")).getAttribute("title"));

driver.navigate().to("http://newtours.demoaut.com/");
driver.findElement(By.name("login")).click();

driver.navigate().to("http://www.seleniumhq.org/");
driver.findElement(By.xpath(".//*[@id='choice']/tbody/tr/td

[2]/center/a/img")).click();

}
}
-----------------------
vi) Handle Link

Operations on Link

> Click
> Check the Link Existence
> Check Enabled status
> Return Link Name etc...

Example:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
//driver.findElement(By.className("gb_P")).click();
//driver.findElement(By.linkText("Gmail")).click();
//driver.findElement(By.partialLinkText("mail")).click();
//driver.findElement(By.cssSelector(".gb_P")).click();
//driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div

[1]/a")).click();
WebElement Gmail_Link = driver.findElement(By.xpath(".//*

[@id='gbw']/div/div/div[1]/div[1]/a"));
boolean linkStatus = Gmail_Link.isDisplayed();
System.out.println(linkStatus);//true

linkStatus = Gmail_Link.isEnabled();
System.out.println(linkStatus);//true

String LinkName = Gmail_Link.getText();
System.out.println(LinkName);

Gmail_Link.click();
}
}
-----------------------
vii) Handle Radio Button

Operations on Radio Button

> Select
> Check Enabled status
> Check Displayed status
> Check Selected status

Example:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?

osCsid=47gtsrhe41613u5r3eqhgdbas7");
WebElement maleRadioButton = driver.findElement(By.xpath(".//*

[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]"));
boolean elementStatus = maleRadioButton.isDisplayed();
System.out.println(elementStatus);//true

System.out.println(maleRadioButton.isEnabled());//true

System.out.println(maleRadioButton.isSelected());//false

maleRadioButton.click();

System.out.println(maleRadioButton.isSelected());//true
}
}
-----------------------
viii) Handle Drop Down box

Operations on Drop Down Box

> Select an Item
> Check Displayed status
> Check Enabled status
> Items count

Example:

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?

osCsid=47gtsrhe41613u5r3eqhgdbas7");
Select dropDown = new Select (driver.findElement(By.name("country")));
//dropDown.selectByIndex(6);//Select an item by index
//dropDown.selectByVisibleText("India");

List<WebElement> e = dropDown.getOptions();
int itemsCount = e.size();
System.out.println(itemsCount);
}
}
-----------------------
ix) Handle Check box

Operations on Check box

> Select
> Unselect
> Check Displayed status
> Check Enabled status
> Check selected status

Example:

WebDriver driver = new FirefoxDriver();
driver.get("file:///E:/HTMLExamples/MultipleCheckbox.html");
System.out.println(driver.findElement(By.xpath("html/body/input

[2]")).isDisplayed());//true

System.out.println(driver.findElement(By.xpath("html/body/input

[2]")).isEnabled());//true

System.out.println(driver.findElement(By.xpath("html/body/input

[2]")).isSelected());//false

driver.findElement(By.xpath("html/body/input[2]")).click();

System.out.println(driver.findElement(By.xpath("html/body/input

[2]")).isSelected());//true

driver.findElement(By.xpath("html/body/input[2]")).click();

System.out.println(driver.findElement(By.xpath("html/body/input

[2]")).isSelected());//false
-----------------------
x) Handle Web Table / HTML Table

Operations on Web Table:

> Get cell value
> Rows Count
> Cells Count

Example:

WebDriver driver = new FirefoxDriver();
driver.get("file:///E:/HTMLExamples/htmlTable.html");
String s = driver.findElement(By.xpath(".//*[@id='students']/tbody/tr[2]/td

[2]")).getText();
System.out.println(s);
WebElement htmlTable = driver.findElement(By.id("students"));

List <WebElement> rows = htmlTable.findElements(By.tagName("tr"));
int r = rows.size();
System.out.println(r);

List <WebElement> cells = htmlTable.findElements(By.tagName("td"));
int c = cells.size();
System.out.println(c);
-----------------------
xi) Handle inline Elements

The span tag is used to group inline Elements in a Document.

Example 1:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
driver.findElement(By.xpath(".//*[@id='gb36']/span[1]")).click();
driver.navigate().back();
-----------------------
Example 2:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
driver.findElement(By.xpath(".//*[@id='gbwa']/div[2]/a[1]")).click();
driver.findElement(By.xpath(".//*[@id='gb300']/span[1]")).click();
-----------------------
Handle Frames
Handle Mouse Over
Working with Multiple browser windows.
-----------------------

0 comments:

Post a Comment