LayoutTitle

Hoverable Dropdown

Friday, September 13, 2019

Web iFrames/Frames Handling - Selenium Web Driver

In most of the GUI applications, you might have not come across with Frames/iFrames mostly but if you are using PEGA applications then definitely you might come across with Frames/iFrames that too more than one frame without no doubt.

The challenge part here is you may be seeing the same object in more than one Frame. This is the situation where automation developer has to break once brain to automate such weird behavior of the application, I mean the complexity of the PEGA application will be like that.

Using Selenium, when Frame/iFrames appears you have to switch to Frame first, then only the specific Object on the desired page will be identified, so that you can action on the Object later on. 

Now, you will learn or get to know more in terms of Selenium code here below.

How to ensure there is a Frame/iFrame present in a web page:

Steps to ensure:

1. Click F12/Go to Developer Tools section
2. In Browser, select Ctrl+F, then find for "//iframe
3. Then you will see number of "iframes" present in the page


How to navigate to Frame/iFrame present in a web page:

Ways to switch Frame are:

1. By Index
2. By Frame Name or ID

Other information/operations on Frames

1. Handling dynamic Frame
2. Handing Child Frame (Multiple Frames)
3. Move back to Parent Frame
4. Move back to Most Parent Frame

By Index:

Examples:

driver.switchTo.frame(0); //switch to index 0
driver.switchTo.frame(1); //switch to index 1
driver.switchTo.frame(2); //switch to index 2

By Frame Name:

Examples:

driver.switchTo.frame("Frame1"); 
driver.switchTo.frame("Frame2"); 
driver.switchTo.frame("Frame3"); 

By Frame ID:

Examples:

driver.switchTo.frame("Frame123CV_0A"); 
driver.switchTo.frame("Frame123CV_1A"); 

driver.switchTo.frame("Frame123CV_2A"); 

Handling dynamic Frame:

Follow the below steps:

1. Get the total no. of Frames present in the particular web page
2. Validate your desired Object present in that web page, repeat the loop until last
3. Later, find the Frame name or Index in which the desired Object present
4. Finally, after obtaining the Frame no., switch to Frame and perform the action.

Example (Steps 1,2 & 3): To get the Frame No of the specific Object

WebElement element=driver.findElement(By.name("userName"));
int size = driver.findElements(By.tagName("iframe")).size();
for (i=0;i<size; i++){
driver.switchTo().parentFrame();

driver.switchTo().frame(i);
try{
if (element.isDisplayed())
   break;
}
catch(Exception e)
{
  e.fillInStackTrace();
}
return i;

}

Example (Step 4): Lets say above Example returned value in frameNo argument and perform the action on the Object.

driver.switchTo().parentFrame();
driver.switchTo().frame(frameNo);
return element.sendKeys("Vasu Chiluveru");  //action on the Web Element

Handling Child Frame (Multiple Frames)

Example:

driver.switchTo().frame("ParentFrameName").switchTo().frame("ChildFrameName");

Move back to Parent Frame

If you want to Move back to the parent frame use below code
driver.switchTo().parentFrame();

Move back to Most Parent Frame

If you want to Move back to most parent frame use below code
driver.switchTo().defaultContent();