Effective Strategies to Stop Page Loading in Selenium WebDriver

Effective Strategies to Stop Page Loading in Selenium WebDriver

3 July 2024 Stephan Petzl Leave a comment QA

When working with Selenium WebDriver, there might be situations where you need to stop a page from loading. This can be particularly useful when dealing with pages that take too long to load or when you need to navigate away from a page quickly. In this article, we will explore various methods to achieve this.

Using JavaScript Executor

One of the most effective ways to stop page loading is by using the JavaScriptExecutor interface provided by Selenium. This approach involves executing JavaScript code to stop the loading process.

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

This method works across different browsers and is straightforward to implement.

Simulating the Escape Key

Another approach is to simulate pressing the Escape key, which is a common way to stop page loading in a browser.


This method is simple and effective, especially when combined with setting the page load strategy to ‘none’ in Chrome.

Setting Page Load Timeout in Firefox

If you are using Firefox, you can set a preference for the default timeout to stop the page from loading after a specified time.

    fp = webdriver.FirefoxProfile()
    fp.set_preference("http.response.timeout", 5)
    fp.set_preference("dom.max_script_run_time", 5)
    driver = webdriver.Firefox(firefox_profile=fp)


This will stop the page load after 5 seconds, providing a controlled way to handle long-loading pages.

Advanced Method: Injecting Scripts into ChromeDriver Session

For more advanced users, injecting a script into the ChromeDriver session can be an effective solution. This method ensures the script runs for every page load.

    driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': 'setTimeout(function(){window.stop();}, 5000);'})

This approach is inspired by the need to handle specific scenarios where other methods might not work effectively.


Stopping a page from loading in Selenium WebDriver can be achieved through various methods, each suited to different scenarios. Whether you use JavaScript execution, simulate key presses, or set browser-specific timeouts, these strategies provide reliable ways to control page loading behavior.

Enhancing Your Testing Workflow with Repeato

When it comes to automating tests for mobile applications, Repeato offers a no-code solution that is both powerful and easy to use. Leveraging computer vision and AI, Repeato allows you to create, run, and maintain automated tests quickly and efficiently. This can be particularly beneficial in ensuring the quality of your applications without the need for extensive coding skills.

Like this article? there’s more where that came from!