How to Stop the page loading in firefox programmatically? How to Stop the page loading in firefox programmatically? selenium selenium

How to Stop the page loading in firefox programmatically?


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


My solution is to use this class:WebDriverBackedSelenium;

//When creating a new browser:WebDriver driver = _initBrowser(); //Just returns firefox WebDriverWebDriverBackedSelenium backedSelenuium =             new WebDriverBackedSelenium(driver,"about:blank");    //This code has to be put where a TimeOut is detected//I use ExecutorService and Future<?> Objectvoid onTimeOut(){    backedSelenuium.runScript("window.stop();");}