如何以编程方式阻止在Firefox中加载页面? [英] How to Stop the page loading in firefox programmatically?

查看:129
本文介绍了如何以编程方式阻止在Firefox中加载页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用WebDriver和Firefox运行几个测试。



我遇到以下命令的问题:

WebDriver。 get(www.google.com);



使用此命令,WebDriver将阻塞,直到onload事件被触发。虽然这通常需要几秒钟的时间,但在完成加载的网站上可能需要几个小时。



我想要做的是在某个超时后停止加载页面,不知何故,模拟火狐的停止按钮。

我第一次尝试执行下面的JS代码,每次我尝试加载一个页面:

  var loadTimeout = setTimeout(\window.stop(); \,10000); 

不幸的是,这不起作用,可能是因为:


由于加载脚本的顺序,stop()方法无法停止加载
一个奇怪的事情,我今天发现是一个网站,从来没有停止加载我的机器上(FF3.6 - 4.0和Mac Os 10.6.7)在其他浏览器和/或计算机上正常加载。

更新2:通过告诉Firefox不加载图像显然可以解决问题。希望以后所有的东西都能正常工作。

我希望WebDriver有一个更好的Chrome驱动来使用它。 Firefox每天都令我失望!

更新3:Selenium 2.9添加了一个新功能来处理驱动程序似乎挂起的情况。这可以与 FirefoxProfile 一起使用,如下所示:

  FirefoxProfile firefoxProfile = new 。ProfilesIni()getProfile( 网); 
firefoxProfile.setPreference(webdriver.load.strategy,fast);

我会在试用后发布这个功能。 b $ b

更新4:在最后没有上述方法的工作。我最终杀死正在花费很长时间完成的线程。我打算尝试 Ghostdriver ,这是一个使用PhantomJS作为后端的远程WebDriver。 PhantomJS是一个无头的WebKit脚本,所以我不希望有像Firefox这样的真正的浏览器的问题。对于那些没有义务使用firefox(抓取目的)的人,我会更新结果。



更新5:更新时间。使用5个月ghostdriver 1.1而不是FirefoxDriver我可以说,我真的很高兴他的表现和稳定。我得到了一些情况,我们没有适当的行为,但一般ghostdriver看起来是足够稳定。所以,如果你需要像我一样的浏览器抓取/网络抓取的目的,我建议你使用ghostdriver,而不是Firefox和xvfb这会给你几个头痛... ...

解决方案

我可以解决这个问题。



首先,设置webdriver的超时时间。例如,

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

其次,当你得到的时候,把它包装在一个TimeoutException中。 (我添加了一个UnhandledAlertException catch,只是为了好的措施。)例如,

  for(int i = 0; i <10 ; i ++){
尝试{
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();






这基本上试图加载页面,但是如果它超时,它迫使页面停止通过JavaScript加载,然后尝试再次获取页面。它可能不会帮助你的情况,但它肯定帮助我,特别是当做一个webdriver的 getCurrentUrl()命令,这也可能需要很长时间,有一个警报,要求网页停止加载之前,你得到的网址。


I am running several tests with WebDriver and Firefox.

I'm running into a problem with the following command:

WebDriver.get(www.google.com);

With this command, WebDriver blocks till the onload event is fired. While this can normally takes seconds, it can take hours on websites which never finish loading.

What I'd like to do is stop loading the page after a certain timeout, somehow simulating Firefox's stop button.

I first tried execute the following JS code every time that I tried loading a page:

var loadTimeout=setTimeout(\"window.stop();\", 10000);

Unfortunately this doesn't work, probably because :

Because of the order in which scripts are loaded, the stop() method cannot stop the document in which it is contained from loading 1

UPDATE 1: I tried to use SquidProxy in order to add connect and request timeouts, but the problem persisted.

One weird thing that I found today is that one web site that never stopped loading on my machine (FF3.6 - 4.0 and Mac Os 10.6.7) loaded normally on other browsers and/or computers.

UPDATE 2: The problem apparently can be solved by telling Firefox not to load images. hopefully, everything will work after that...

I wish WebDriver had a better Chrome driver in order to use it. Firefox is disappointing me every day!

UPDATE 3: Selenium 2.9 added a new feature to handle cases where the driver appears to hang. This can be used with FirefoxProfile as follows:

FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("web");
firefoxProfile.setPreference("webdriver.load.strategy", "fast");

I'll post whether this works after I try it.

UPDATE 4: at the end none of the above methods worked. I end up "killing" the threads that are taking to long to finish. I am planing to try Ghostdriver which is a Remote WebDriver that uses PhantomJS as back-end. PhantomJS is a headless WebKit scriptable, so i expect not to have the problems of a real browser such as firefox. For people that are not obligate to use firefox(crawling purposes) i will update with the results

UPDATE 5: Time for an update. Using for 5 months the ghostdriver 1.1 instead FirefoxDriver i can say that i am really happy with his performance and stability. I got some cases where we have not the appropriate behaviour but looks like in general ghostdriver is stable enough. So if you need, like me, a browser for crawling/web scraping purposes i recomend you use ghostdriver instead firefox and xvfb which will give you several headaches...

解决方案

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.

这篇关于如何以编程方式阻止在Firefox中加载页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆