How to navigate back to current page from Frame in selenium webdriver using Java? Last Updated : 10 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.PrerequisiteWe 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:OutputConclusionNavigating 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. Comment More infoAdvertise with us Next Article How to Click on a Hyperlink Using Java Selenium WebDriver? A agrawalvishesh9271 Follow Improve Article Tags : Software Testing Selenium Similar Reads How to Take a Screenshot in Selenium WebDriver Using Java? Selenium WebDriver is a collection of open-source APIs used to automate a web application's testing. To capture a screenshot in Selenium, one must utilize the Takes Screenshot method. This notifies WebDriver that it should take a screenshot in Selenium and store it. Selenium WebDriver tool is used t 3 min read How to Force Selenium WebDriver to Click on Element which is Not Currently Visible? A programming language that is used to execute a set of instructions that a computer can understand and execute to perform various tasks is known as Java. Java can be controlled autonomously using various automation tools. Table of Content Visibility criteriaWait for VisibilityScroll the Element int 7 min read How to Click on a Hyperlink Using Java Selenium WebDriver? An open-source tool that is used to automate the browser is known as Selenium. Automation reduces human effort and makes the work comparatively easier. There are numerous circumstances in which the user wants to open a new page or perform a certain action with the click of the hyperlink. In this art 4 min read How to get text from the alert box in java Selenium Webdriver? In Automation testing, we can capture the text from the alert message in Selenium WebDriver with the help of the Alert interface. By default, the webdriver object has control over the main page, once an alert pop-up gets generated, we have to shift the WebDriver focus from the main page to the alert 2 min read How to do session handling in Selenium Webdriver using Java? In Selenium WebDriver, managing browser sessions is crucial for ensuring that each test runs independently and without interference. A browser session in Selenium is identified by a unique session ID, which helps track and manage the session throughout the test. Proper session handling in Selenium W 4 min read How to Save a Web Page with Selenium using Java? Selenium is widely known for automating browser interactions, but it can also be used to save web pages directly. This capability is particularly useful when you need to archive web content, save dynamic pages that change frequently, or scrape and store HTML for later analysis. In this tutorial, weâ 3 min read Like