Handling Elements in Selenium Part-3


Handling Elements in Selenium Part-3

In Handling Elements in Selenium Part-1

i) Handle Browser

ii) Handle Edit box

iii) Handle Text Area, Error Message, Window Dialog

iv) Handle Button
-----------------------
In Handling Elements in Selenium Part-2

v) Handle Images (General Image, Image Button and Image Link)

vi) Handle Link

vii) Handle Radio Button

viii) Handle Drop down box

ix) Handle Check box

x) Handle Web Table / HTML Table

xi) Handling inline Elements
-----------------------
Handling Elements in Selenium Part-3

xii) Handle Frames

> HTML frames are used to divide the Browser window into multiple sections, where each section can load a separate HTML document.

> Frames are sections of Web page displayed on top window.

> Whenever we access the page then focus on the top window.

Switch to a frame is done in two ways

1) Using frame index

Syntax:

driver.swithchTo().frame(int index);

Example:

driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.switchTo().frame(2);
driver.findElement(By.xpath("html/body/div[3]/table/tbody[2]/tr[1]/td[1]/a")).click();

2) Using frame name

Syntax:

driver.switchTo().frame(String frame Name);

Example:

driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.switchTo().frame("classFrame");
driver.findElement(By.xpath("html/body/div[3]/table/tbody[2]/tr[1]/td[1]/a")).click();
-----------------------
> Top window to a Frame (frame index/frame name)

> Switch from a frame to Top window

Syntax:

driver.switchTo().defaultContent();
---------------------
Navigation:

> Launch the page
> Switch to 3rd frame
> Operate an element
> Back to Top window
> Switch to 1st frame
> Operate an element
--------------------------
Example:

WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");

//Switch to 3rd frame
driver.switchTo().frame(2);
driver.findElement(By.linkText("com.thoughtworks.selenium")).click();
Thread.sleep(3000);
//Switch from 3rd frame to Top window
driver.switchTo().defaultContent();
Thread.sleep(3000);
//Switch to 1st frame
driver.switchTo().frame(0);
driver.findElement(By.linkText("org.openqa.selenium")).click();
-----------------------
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
Thread.sleep(3000);
//Switch to 3rd frame
driver.switchTo().frame("classFrame");
driver.findElement(By.linkText("com.thoughtworks.selenium")).click();
Thread.sleep(3000);
//Switch from 3rd frame to Top window
driver.switchTo().defaultContent();
Thread.sleep(3000);
//Switch to 1st frame
driver.switchTo().frame("packageListFrame");
driver.findElement(By.linkText("org.openqa.selenium")).click();
}
}
-----------------------
xiii) Handle Mouse hover
 

WebDriver driver = new FirefoxDriver();
driver.get("http://www.carmax.com/");
//create Action builder instance by passing WebDriver instance
Actions builder = new Actions(driver);
WebElement menuElement = driver.findElement(By.linkText("Sell Us Your Car"));
builder.moveToElement(menuElement).build().perform();
driver.findElement(By.linkText("FAQ")).click();
-----------------------
xiv) Handle Multiple Browsers

String parent = driver.getWindowHandle();
//System.out.println(parent);

Set <String> Handles = driver.getWindowHandles();
int BrowserCount = Handles.size();
System.out.println(BrowserCount);

for (String s1:Handles){
if (! s1.equals(parent)){
driver.switchTo().window(s1);
System.out.println(driver.getCurrentUrl());
}
}
driver.switchTo().window(parent);
System.out.println(driver.getCurrentUrl());
-----------------------
xv) Handle Duplicate objects

Redirecting to GCR Shop User Interface from Admin Interface
(After Login to Admin Interface then Redirect to User Interface)

Example:

driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys("admina");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
driver.findElement(By.linkText("Online Catalog")).click();
}
System.out.println(driver.getCurrentUrl());
}
}
-----------------------
Assignment:

Enter Password in to 2nd Password Edit box in www.infibeam.com Registration page.
-----------------------

0 comments:

Post a Comment