How to stop a page loading from Selenium in chrome?

Last Updated : 26 Oct, 2026

In Selenium, some webpages may take longer to load due to large images, advertisements, JavaScript files, or slow network responses. Selenium provides methods to stop further page loading and control how long WebDriver waits during navigation.

  • The window.stop() method stops further loading of resources on the current webpage.
  • PageLoadStrategy controls when Selenium continues after navigation.
  • Page load timeouts and explicit waits help handle slow-loading webpages.

Why Stop Page Loading in Selenium?

Stopping unnecessary resource loading or reducing navigation wait times can improve the efficiency of Selenium tests.

  • Continue automation without waiting for unnecessary resources to finish loading.
  • Limit the time Selenium spends waiting for slow or unresponsive pages.
  • Interact with available elements without requiring every resource to finish loading.

1. Stop Page Loading Using JavaScript

The window.stop() method stops further loading of resources in the current browser window, similar to clicking Chrome's Stop button. Selenium executes this method using the JavascriptExecutor interface.

Syntax:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.stop();");

Example: Stop Page Loading in Chrome

The following program uses PageLoadStrategy.EAGER to continue when the DOM is ready and then executes window.stop() to stop remaining resource loading.

Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Geeks {
    public static void main(String[] args) {

        // Continue navigation when the DOM is ready
        ChromeOptions options = new ChromeOptions();
        options.setPageLoadStrategy(PageLoadStrategy.EAGER);

        WebDriver driver = new ChromeDriver(options);

        try {
            // Open the webpage
            driver.get("https://www.geeksforgeeks.org/");

            // Stop loading remaining resources
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("window.stop();");

            System.out.println("Stop command executed");

        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

Output:

Stop command executed

Explanation: PageLoadStrategy.EAGER allows Selenium to continue once the DOM is ready. The window.stop() method then requests the browser to stop loading any remaining page resources.

Note: If the webpage has already finished loading, window.stop() has no remaining resources to stop. It also does not guarantee that all background network requests will terminate.

2. Control Page Loading Using PageLoadStrategy

Selenium provides three page load strategies to determine when WebDriver returns from navigation.

  • NORMAL: Waits until the document reaches the complete state.
  • EAGER: Waits until the document reaches the interactive state, without requiring all resources to finish loading.
  • NONE: Returns without waiting for the document to reach a particular readiness state.

Unlike window.stop(), these strategies do not stop resource loading. They only control how long WebDriver waits during navigation.

3. Handling Slow Page Loads in Selenium

Slow-loading webpages may cause Selenium tests to fail when navigation takes too long or required elements are unavailable. Selenium provides pageLoadTimeout() to limit navigation waiting time and WebDriverWait to wait for specific elements.

Example: Handling Slow Page Loads

The following program sets a page load timeout and waits for the Google search box to become visible.

Java
import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Geeks {
    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

        try {
            // Set maximum navigation wait
            driver.manage().timeouts()
                  .pageLoadTimeout(Duration.ofSeconds(10));

            // Open the webpage
            driver.get("https://www.google.com/");

            // Wait for the search box to become visible
            WebDriverWait wait = new WebDriverWait(
                driver, Duration.ofSeconds(10));

            wait.until(
                ExpectedConditions.visibilityOfElementLocated(
                    By.name("q")));

            System.out.println("Search box is visible");

        } catch (TimeoutException e) {
            System.out.println("Navigation or element wait timed out");

        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

Output:

Search box is visible

If navigation or the element wait exceeds its timeout:

Navigation or element wait timed out

Explanation: The program allows up to 10 seconds for navigation and then up to 10 seconds for the search box to become visible. If either operation exceeds its timeout, Selenium throws a TimeoutException. These are separate waiting periods.

Best Practices for Managing Page Loads

Following these best practices helps reduce unnecessary loading delays and improve the reliability and efficiency of Selenium tests.

  • Use PageLoadStrategy.EAGER when the DOM is sufficient for test execution.
  • Use window.stop() only when remaining resource loading needs to be interrupted.
  • Set reasonable navigation timeouts to avoid excessive waiting.
  • Prefer explicit waits over fixed delays such as Thread.sleep() when interacting with webpage elements.
Comment

Explore