
Frames
In this section, we will learn about the frames in a web page and how to identify the frames. Also, we will find out how we can handle a frame in Selenium WebDriver.
Many developers like to place elements inside a frame. The frame is just like a container where few elements can be grouped.
Identification of a frame:
Different ways to know if the element is present inside a frame or not
#1. Right-click on the element. Check if “This Frame” option is available. If This frame option is available, it means that the element is inside a frame.
#2. View page source of the web page and check if any tag is available for ‘iframe’.
Verify Number of frames in a webpage:
All the frames are having the tag name as “iframe”.
List<WebElement> frameList=driver.findElements(By.tagName(“iframe”));
System.out.println(frameList.size());
In above example: frameList will have all the list of frames and frameList.size() will give the number of frames.
Handling an element inside the frame:
If an element is inside a frame then control has to switch to frame first and then start operating on the elements.
Step 1: To switch inside a frame:
driver.switchTo().frame(1); //pass frame number as parameter.
or
driver.switchTo().frame(“frame Name”); //pass frame name as parameter.
or
driver.switchTo().frame(“xpath of the frame”);
Step 2: After switching inside a frame selenium will be able to operate on elements.
driver.findElement(//*[@id=’username’]).sendKeys(“username”);
driver.findElement(//*[@id=’pass’]).sendKeys(“password”);