How To Handle Elements In Frame – Selenium

How To Handle Elements In Frame - Selenium

A frame is another section in the web page which is inherited in the document object model(DOM) or page. So it is starts with another <html> and <body> section so we can say that a frame is another page within the web page itself.

Handling an element in selenium on the normal web page and DOM is different than handling element in the frame.

There are various methods in selenium to handle the element within frame!!!

If you are not aware of frames read here What Is Frame?

So let us start and explore the handling of the frame element.

How To Handle Elements In Frame - Selenium

A frame is another web page sub-section which is actually a page itself. Selenium web driver element finder cannot find these types of elements within a frame so we can switch to that frame before handling elements of a frame that is the only way to handle element of a frame even you can handle any type of element within this frame.

General Error: Selenium web throwing an error ”

Solution: Switching into frame Through Index/Id/WebElement/name and then find the element within frame itself just like the page

In selenium web driver there are three ways to Handle Elements In Frame:

  1. By Index
  2. By Name Or ID
  3. By WebElement

1. By Index: By index, you can switch to the frame, below is the syntax:

driver.switchTo().frame(int index)

driver : Interface

switchTo(): Method

frame() : Method

examples:

  • driver.switchTo(0)  – first frame
  • driver.switchTo(1) –  second frame

2. By Name/Id: Element can also be find through the name/id of the frame, below is the syntax

  • driver.switchTo().frame(“Name”)
  • driver.switchTo().frame(“Id”)

3. By WebElement: Element can be find in the frame through webelement

  • driver.switchTo().frame(WebElement element)

Finding size of the frame: int length=driver.findElement(By.tagName(“iframe”));

Learn Selenium Automation Testing Online From Your Home – Selenium 

Below is the sample code of firstcry in which login box is in the frame that’s why code has to switch into frame and then hitting the element within that frame


WebDriver driver=new FirefoxDriver();
driver.get(“http://www.firstcry.com/”);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath(“//div[5]/div[1]/div/div[2]/span[5]”)).click();
driver.switchTo().frame(“iframe_Login”);   // switching into frame
driver.findElement(By.xpath(“//input[@id=’txtLUNm’]”)).sendKeys(“mailtestingclass@gmail.com”);


Sytax to switch back to parent window:

  • driver.switchTo().parentFrame();
  • driver.switchTo().defaultContent();

Hope this tutorial helps you in handling frame elements, if you are stuck in any coding part of frame or any error occurred in your program do comment in the below comment box so that would try to solve.

Learn Selenium Automation Testing Online From Your Home – Selenium 

For Your Success,

Adil

 

Adil

My Name is Mohammad Adil and I am a tech enthusiastic, a technical blogger, motivational speaker, trainer and passionate quality analyst with over 5+ years of experience in software testing field as well as information technology field. I worked with various reputed CMM level companies and currently working with a well-reputed product development company.