How to Stop the page loading in firefox programmatically?

醉酒当歌 提交于 2019-11-28 21:24:10

I was able to get around this doing a few things.

First, set a timeout for the webdriver. E.g.,

WebDriver wd;
... initialize wd ...
wd.manage().timeouts().pageLoadTimeout(5000, TimeUnit.MILLISECONDS);

Second, when doing your get, wrap it around a TimeoutException. (I added a UnhandledAlertException catch there just for good measure.) E.g.,

for (int i = 0; i < 10; i++) {
    try {
        wd.get(url);
        break;
    } catch (org.openqa.selenium.TimeoutException te) {
        ((JavascriptExecutor)wd).executeScript("window.stop();");
    } catch (UnhandledAlertException uae) {
        Alert alert = wd.switchTo().alert();
        alert.accept();
    }
 }

This basically tries to load the page, but if it times out, it forces the page to stop loading via javascript, then tries to get the page again. It might not help in your case, but it definitely helped in mine, particularly when doing a webdriver's getCurrentUrl() command, which can also take too long, have an alert, and require the page to stop loading before you get the url.

I've run into the same problem, and there's no general solution it seems. There is, however, a bug about it in their bug tracking system which you could 'star' to vote for it.

http://code.google.com/p/selenium/issues/detail?id=687

One of the comments on that bug has a workaround which may work for you - Basically, it creates a separate thread which waits for the required time, and then tries to simulate pressing escape in the browser, but that requires the browser window to be frontmost, which may be a problem.

http://code.google.com/p/selenium/issues/detail?id=687#c4

Vincent Bouvier

My solution is to use this class: WebDriverBackedSelenium;

//When creating a new browser:
WebDriver driver = _initBrowser(); //Just returns firefox WebDriver
WebDriverBackedSelenium backedSelenuium = 
            new WebDriverBackedSelenium(driver,"about:blank");    

//This code has to be put where a TimeOut is detected
//I use ExecutorService and Future<?> Object

void onTimeOut()
{
    backedSelenuium.runScript("window.stop();");
}

One weird thing that i found today is that one web site that never stop loading on my machine (FF3.6 - 4.0 and Mac Os 10.6.7), is stop loading NORMALy in Chrome in my machine and also in another Mac Os and Windows machines of some colleague of mine!

I think the problem is closely related to Firefox bugs. See this blog post for details. Maybe upgrade of FireFox to the latest version will solve your problem. Anyway I wish to see Selenium update that simulates the "stop" button...

Basically I set the browser timeout lower than my selenium hub, and then catch the error. And then stop the browser from loading, then continue the test.

webdriver.manage().timeouts().pageLoadTimeout(55000);

function handleError(err){
          console.log(err.stack);
        };


return webdriver.get(url).then(null,handleError).then(function () {
            return webdriver.executeScript("return window.stop()");
        });

Well , the following concept worked with me on Chrome , try the same:

1) Navigate to "about:blank" 2) get element "body" 3) on the elemënt , just Send Keys Ësc

It was a really tedious issue to solve. However, I am wondering why people are complicating it. I just did the following and the problem got resolved (perhaps got supported recently):

driver= webdriver.Firefox()
driver.set_page_load_timeout(5)
driver.get('somewebpage')

It worked for me using Firefox driver (and Chrome driver as well).

Just in case someone else might be stuck with the same forever loading annoyance, you can use simple add-ons such as Killspinners for Firefox to do the job effortlessly.

Edit : This solution doesn't work if javascript is the problem. Then you could go for a Greasemonkey script such as :

// ==UserScript==
// @name        auto kill
// @namespace   default
// @description auto kill
// @include     *
// @version     1
// @grant       none
// ==/UserScript==

function sleep1() {
  window.stop();
  setTimeout(sleep1, 1500);
}

setTimeout(sleep1, 5000);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!