How to navigate back to current page from Frame in selenium webdriver using Java?
Last Updated :
23 Jul, 2025
When automating tests in Selenium WebDriver using Java, working with frames can be tricky. Frames are separate sections within a webpage that operate independently of the main content. Navigating between frames and the main page is a common task in web automation testing. Understanding how to switch from a frame back to the main page or another frame is essential.
This ability helps testers interact seamlessly with different parts of the web page, ensuring thorough validation of web applications. Let’s explore how to navigate back to the current page from a frame in Selenium WebDriver.
Prerequisite
We will be required 3 main things:
- Java Development Kit (JDK) installed.
- Browser Driver (e.g., ChromeDriver for Chrome).
- IDE like Eclipse or IntelliJ IDEA.
Let us take an Example:
Frames are HTML elements that allow embedding another HTML document inside the parent page. In Selenium WebDriver, navigating between frames and the main page (also called the parent page) is crucial when automating interactions with web pages containing multiple frames or iframes. WebDriver provides methods to switch between frames and back to the main content. Failing to switch back to the parent page after interacting with a frame can cause errors when accessing elements outside the frame. We can use the driver.switch () to switch to the target frame.frame() method.
Iframe Inspect Element
SwitchBackFrame.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class SwitchBackFrame {
public static void main(String[] args) {
// Set the path to the ChromeDriver executable manually
System.setProperty("webdriver.chrome.driver", "path_to_your_chromedriver_executable");
// Initialize ChromeDriver instance
WebDriver driver = new ChromeDriver();
try {
// Navigate to the test page
driver.get("https://the-internet.herokuapp.com/frames");
// Set implicit wait
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
// Identify and click the "Nested Frames" link
driver.findElement(By.partialLinkText("Nested")).click();
// Switch to the frame by frame name ("frame-bottom")
driver.switchTo().frame("frame-bottom");
// Locate the body element inside the frame and print its text
WebElement frameBody = driver.findElement(By.cssSelector("body"));
System.out.println("Frame text: " + frameBody.getText());
// Switch back to the main page content
driver.switchTo().defaultContent();
} finally {
// Close the browser
driver.quit();
}
}
}
Output:
OutputConclusion
Navigating between frames and the main content in Selenium WebDriver is a key skill when testing web applications that use frames. Using the switchTo().defaultContent()
method allows you to return to the main content from any frame, while switchTo().parentFrame()
takes you back to the parent frame.
This flexibility ensures that your Selenium automation scripts can handle complex page structures smoothly, improving test efficiency and reliability. Mastering this will help in writing more robust and effective web automation tests.