Working with frames,frameSet,iframes in Selenium WebDriver using CoreJava(TargetLocator)
Handling Frames using TargetLocator(switchTo())!!!
Generally when ever we open the page we don't find the frames for every application. Ex: Facebook.com doesn't have frames. If we want to confirm whether frames are implemented for the web application we can follow
steps mentioned below:
1. Open Firefox
2. Enter the Url http://www.facebook.com
3. Click on Firebug plugin.
4. Navigate to Firepath plugin.
5. Check the screenshot as mentioned below. Based on this we can decide whether the frames are implemented for this application.
Also, if we want to perform any action say enter the text in the textbox available in the Frame. we have to use following methods:
driver.switchTo().frame(int index)- index starts from zero.
or
driver.switchTo().frame(String framename)
Download the html source code from these links:
1. Frames.html
https://tobeanexpert.blogspot.com/b/post-preview?token=w7uC8EwBAAA.5IP50O0wZ0fsnoaOe5jiLQ.lYohA8_aM5FeDVfOJAyW_A&postId=629786504598294227&type=POST
2. frame_a.html
https://tobeanexpert.blogspot.com/b/post-preview?token=DHWE8EwBAAA.5IP50O0wZ0fsnoaOe5jiLQ.OU_N6UQ4tzlB7XypI_Se_A&postId=1277459784965833367&type=POST
3. frame_b.html
https://tobeanexpert.blogspot.com/b/post-preview?token=cluH8EwBAAA.5IP50O0wZ0fsnoaOe5jiLQ.1JCsNYQVBnWsiXWYmOcl1Q&postId=3121611285388662540&type=POST
4. frame_c.html
https://tobeanexpert.blogspot.com/b/post-preview?token=vo6H8EwBAAA.5IP50O0wZ0fsnoaOe5jiLQ.DjH_UeWElg9eQ_06IQETBw&postId=7113507276382232086&type=POST
After downloading the html examples from the (1-4) links opening the Frames.html it looks like web-application the finally as below
Program: HandlingFrames.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HandlingFrames {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("file:///D:/Selenium/HTML/Frames.html");
//switch to a particular frame from the topwindow using index
driver.switchTo().frame(2);
driver.findElement(By.id("Frame3txt")).clear();
driver.findElement(By.id("Frame3txt")).sendKeys("in frame3");
//Switches the frame to a top window
driver.switchTo().defaultContent();
driver.switchTo().frame("frame_a");
//driver.switchTo().frame(driver.findElement(By.id("Frame1txt")));
driver.findElement(By.id("Frame1txt")).clear();
driver.findElement(By.id("Frame1txt")).sendKeys("in frame1");
//Switches the frame to a top window
driver.switchTo().defaultContent();
driver.switchTo().frame("frame_b");
driver.findElement(By.id("Frame2txt")).clear();
driver.findElement(By.id("Frame2txt")).sendKeys("in frame2");
}
}
Handling iFrames using TargetLocator(switchTo())!!!
Usually idenfitying Frames is easy becoz when defining the frames the developer provides the properties of the frame say name and id.
In the below examples the frame name is provided which is easy we can switch using driver.switchTo().frame("frame_a");
<!DOCTYPE html> <html> <frameset cols="25%,*,25%"> <frame src="frame_a.html" name="frame_a"> <frame src="frame_b.html" name="frame_b"> <frame src="frame_c.html" name="frame_c"> </frameset> </html>But in the case of iframe its totally different. No properties defined for iframe then how do we switch???
<!DOCTYPE html> <html> <body> <iframe width="100%" height="300px" src="iframedemo.htm"> </iframe> </body> </html>To understand this better check the page source of the html file "iframedemo.htm". It contains one textbox box named "frame1txt".
Now we can switch to this iframe using the following method:
driver.switchTo().frame(driver.findElement(By.name("frame1txt")));
<html> <body style="background-color:#F5DEB3"> <h1>This page is displayed in an iframe</h1> Username:<input type="text" name="frame1txt" id="frame1txt"> </body> </html>Download the html source code from these links:
1. Handlingiframes.html
2. iframedemo.htm
Program: HandlingIFrames.java
public class HandlingIFrames {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("file:///D:/Selenium/HTML/Frames.html");
//switch to a particular iframe using the webElement
driver.switchTo().frame(driver.findElement(By.name("frame1txt")));
driver.findElement(By.name("frame1txt")).sendKeys("selenium");
}
}
References:http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.TargetLocator.html