Category:
Selenium WebDriver
iFrame is a HTML document embedded inside an HTML document. iFrame is defined by an tag in HTML. With this tag, you can identify an iFrame while inspecting the HTML tree.
To Switch between iFrames we have to use the driver’s switchTo().frame command.
We can use the switchTo().frame() in three ways:
- switchTo.frame(int frameNumber): Pass the frame index and driver will switch to that frame.
- switchTo.frame(string frameName): Pass the frame element Name or ID and driver will switch to that frame.
- switchTo.frame(WebElement frameElement): Pass the frame web element and driver will switch to that frame.
//Switch by frame name
driver.switchTo().frame(“iframe1”);
//Switch by frame ID
driver.switchTo().frame(“IF1”);
//First find the element using any of locator stratedgy
WebElement iframeElement = driver.findElement(By.id(“IF1”));
//switch to frame by using frame element
driver.switchTo().frame(iframeElement);
//Switch back to the main window
driver.switchTo().defaultContent();