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.
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:
- By Index
- By Name Or ID
- By WebElement
1. By Index: By index, you can switch to the frame, below is the syntax:
driver : Interface
frame() : Method
- 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
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”));
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.switchTo().frame(“iframe_Login”); // switching into frame
Sytax to switch back to parent window:
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.
For Your Success,