How to handle frame in selenium WebDriver?

In this article, i will talk about how to handle frame in selenium webdriver.

What is frame?

Frame is nothing but a container where few elements are grouped together. In short Frame is a HTML document within HTML document (Page). In HTML frames are also called as Iframe (Inline Frame). On HTML pages, iframes are mostly used to add content from another source (page), like advertisement.

How to Identify if frame is present on WebPage?

If you are unable to click on the element directly, please check if it is a frame. It is very simple to identify if it is a frame.
Right click on the frame. IF you see option like “This Frame” present after right click, then that is a frame & you need to handle it by switching it to that frame as explained further.

Below Image shows you how to identify frame.

On my blog : https://automationtalks.com/  My name card of LinkedIn appear as frame. refer below screenshot

If frame is there, means it should has either id or name. So that this frame id or frame name will be used to switch to this frame. Below image shows about frame id and frame name.
How to find total number of frames present on the webpage? 
Let us understand how can we find out how many frames are present on particular webpage.

As we saw earlier, the frame will have tagName “iFrame”. So we can use this tagname to find out number of frames present on the WebPage.

Get the number of frames in the list.

Example

List<WebElement> frameList=driver.findElements(By.tagName(“iframe”));
System.out.println(“Total number of frames present: ”+frameList.size());
Once we get to know about the frame, now question is how to handle this frame.

In order to deal with the elements present on the frame, first we need to switch to that respective frame. Let us see how this can be done

Scenario 1: If we know the number of frames present on the page-

If you know about number of frames present on the webpage, and you are sure about the frame number to which you wanted to switch, we can use index number (frame number) to switch to frame.

Note that, frames index number starts from 0. This means, for first frame, index would be 0.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");
		
		  try {
			  
			  driver.switchTo().frame(indexnumber); //here you can enter the index number
			  
			       }
			 catch (NoSuchFrameException e)
			      {            
			  
	System.out.println(e.getMessage());     
	
		
	}

Scenario 2: if you know the frame name.

If you know the frame name, How to handle frame, we can use –

                 WebDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		
		driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");
		
		try {
			driver.switchTo().frame("frameName"); //enter the frame name here
			      } 
			 catch (NoSuchFrameException e)
			    {            
			    System.out.println(e.getMessage());     
			    }

Scenario 3: if you don’t know about number of frames and frame names.
If you are not sure about the frame id or frame name, then the only option you have is, using xpath.

WebDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		
		driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");
	
		 try {
		 
		WebElement button=driver.findElement(By.xpath(""));  
		 
		driver.switchTo().frame(button);		 
		}
		catch (NoSuchFrameException e)
		     {          
		System.out.println(e.getMessage());     
		 
		     }
Note that, once you are done with operations on the frame, you need to switch back to you main page, using defaultContent menthod.

  driver.switchTo().defaultContent();
Hope, this makes you clear about how to handle frame !!!!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories