Concept of Nested Frames(Frames inside Frames)
Let's assume that there are two frames one inside other like
shown in below image and our requirement is printing the text in the outer
frame and inner frame.
In the case of nested frames,
·
At first we must switch to the outer frame by either Index or ID
of the iframe
·
Once we switch to the outer frame we can find the total number
of iframes inside the outer frame, and
·
We can switch to the inner frame by any of the known methods.
While exiting out of the frame, we must exit out in the same
order as we entered into it from the inner frame first and then outer frame.
The Html code for the
above nested frame is as shown below.
The above HTML code
clearly explains the iframe tag (highlighted in green) within another iframe
tag, indicating presence of nested iframes.
Step 1)
WebDriver driver=new FirefoxDriver();
driver.get("Url");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
int size =
driver.findElements(By.tagName("iframe")).size();
System.out.println("Total Frames --" + size);
// prints the total number of
frames
driver.switchTo().frame(0); //
Switching the Outer Frame
System.out.println (driver.findElement(By.xpath("xpath of the outer
element ")).getText());
·
Switch to the outer Frame.
·
Prints the text on outer frame.
Once we switch to the outer frame, we should know whether any
inner frame present inside the outer frame
Step 2)
size
= driver.findElements(By.tagName("iframe")).size();
// prints the total number of frames inside
outer frame
System.out.println("Total Frames
--" + size);
·
Finds the total number of iframes inside outer frame.
·
If size was found '0' then there is no inner frame inside the
frame.
Step 3)
driver.switchTo().frame(0);
// Switching to innerframe
System.out.println(driver.findElement(By.xpath("xpath
of the inner element ")).getText());
·
Switch to the inner frame
·
Prints the text on the inner frame.
Here is the complete code:
public
class FramesInsideFrames {
public
static void main(String[] args) {
WebDriver
driver=new FirefoxDriver();
driver.get("Url");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
int size = driver.findElements(By.tagName("iframe")).size();
System.out.println("Total Frames --" + size);
// prints the total number of
frames
driver.switchTo().frame(0); //
Switching the Outer Frame
System.out.println (driver.findElement(By.xpath("xpath of the outer
element ")).getText());
//Printing the text in outer
frame
size =
driver.findElements(By.tagName("iframe")).size();
// prints the total number of frames inside outer frame
System.out.println("Total Frames --" + size);
driver.switchTo().frame(0); // Switching to innerframe
System.out.println(driver.findElement(By.xpath("xpath of the inner
element ")).getText());
//Printing the text in inner
frame
driver.switchTo().defaultContent();
}
}
Output:
The output of the above code would print the text in the Inner
frame and Outer frame.


Comments
Post a Comment