Working on Frames:
===============
Frame is an html document which is embedded into another
html doc (i.e. page within a Page)
Advantage of Frames is that we can update any code within a frame without impacting other code on page
Q: How to findout if a webElement is present within a Frame or not?
Right click on webelement, if we get "This frame" option
that means the webelement is present within a frame
or
if that webelement is existing under "frame" or"iframe" tag
Exp: Write script to find number of frames present in a page and to read each frame name
Script:
WebDriver driver= new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.manage().window().maximize();
//to find number of Frames in a page
List frames=driver.findElements(By.tagName("frame"));
System.out.println("Number of Frames are: "+frames.size());
//to read each frame name
for(WebElement frame: frames){
System.out.println(frame.getAttribute("name"));
}
Q: How to work on a Webelement within a Frame?
Firstly, we need a "switch" get into the frame, then we can specify
operation on webElement within that frame
We can switch between frames by using below commands.
i. driver.switchTo().frame(name/Id);
Name property or ID property of the frame.
Name is most suggested locator to identify a frame
ii. driver.switchTo().frame(index);
Index means, if we have 3 frames in a page to identify those frames
index will be - 0,1,2
Exp: Write script to click on "org.openqa.selenium.chrome" link under "classFrame" frame
WebDriver driver= new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.manage().window().maximize();
driver.switchTo().frame("classFrame");
driver.findElement(By.linkText("org.openqa.selenium.chrome")).click();
Exp: Write script for following scenario
-open application "https://netbanking.hdfcbank.com"
-click on "continue" button
-read msg from alert
-close alert
-enter "123456" in Userid edit box
-close browser
Script:
WebDriver driver = new FirefoxDriver();
driver.get("https://netbanking.hdfcbank.com");
driver.manage().window().maximize();
//to switch to frame
driver.switchTo().frame("login_page");
//to click on "Continue"
driver.findElement(By.xpath("//img[@alt='continue']")).click();
Thread.sleep(3000);
//to read msg from popup
String eMsg=driver.switchTo().alert().getText();
System.out.println(eMsg);
//to close popup
driver.switchTo().alert().accept();
//to enter value in editbox
driver.findElement(By.name("fldLoginUserId")).sendKeys("123456");
//to close browser
driver.quit();
===============
Frame is an html document which is embedded into another
html doc (i.e. page within a Page)
Advantage of Frames is that we can update any code within a frame without impacting other code on page
Q: How to findout if a webElement is present within a Frame or not?
Right click on webelement, if we get "This frame" option
that means the webelement is present within a frame
or
if that webelement is existing under "frame" or"iframe" tag
Exp: Write script to find number of frames present in a page and to read each frame name
Script:
WebDriver driver= new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.manage().window().maximize();
//to find number of Frames in a page
List
System.out.println("Number of Frames are: "+frames.size());
//to read each frame name
for(WebElement frame: frames){
System.out.println(frame.getAttribute("name"));
}
Q: How to work on a Webelement within a Frame?
Firstly, we need a "switch" get into the frame, then we can specify
operation on webElement within that frame
We can switch between frames by using below commands.
i. driver.switchTo().frame(name/Id);
Name property or ID property of the frame.
Name is most suggested locator to identify a frame
ii. driver.switchTo().frame(index);
Index means, if we have 3 frames in a page to identify those frames
index will be - 0,1,2
Exp: Write script to click on "org.openqa.selenium.chrome" link under "classFrame" frame
WebDriver driver= new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.manage().window().maximize();
driver.switchTo().frame("classFrame");
driver.findElement(By.linkText("org.openqa.selenium.chrome")).click();
Exp: Write script for following scenario
-open application "https://netbanking.hdfcbank.com"
-click on "continue" button
-read msg from alert
-close alert
-enter "123456" in Userid edit box
-close browser
Script:
WebDriver driver = new FirefoxDriver();
driver.get("https://netbanking.hdfcbank.com");
driver.manage().window().maximize();
//to switch to frame
driver.switchTo().frame("login_page");
//to click on "Continue"
driver.findElement(By.xpath("//img[@alt='continue']")).click();
Thread.sleep(3000);
//to read msg from popup
String eMsg=driver.switchTo().alert().getText();
System.out.println(eMsg);
//to close popup
driver.switchTo().alert().accept();
//to enter value in editbox
driver.findElement(By.name("fldLoginUserId")).sendKeys("123456");
//to close browser
driver.quit();
No comments:
Post a Comment