Selenium WebDriver 无需等待页面加载即可进入页面 [英] Selenium WebDriver go to page without waiting for page load

查看:49
本文介绍了Selenium WebDriver 无需等待页面加载即可进入页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python api 将一些 Selenium RC 测试翻译成 Selenium WebDriver.在 Selenium WebDriver 中,我注意到 driver.get( 'http://...' ) 似乎在继续之前等待整个页面加载.有没有办法不等待页面加载?我请求的某些页面有很多外部资源,可能需要很长时间才能加载.我宁愿等待 DOM 上的元素出现,也不愿等待所有内容都加载完毕.由于这个原因,我的一些测试在 WebDriver 中似乎需要两倍的时间.

I'm translating some Selenium RC tests to Selenium WebDriver using the python api. In Selenium WebDriver, I'm noticing that driver.get( 'http://...' ) seems to wait for the entire page to load before proceeding. Is there a way to not wait for a page to load? Some of the pages I'm requesting have a lot of external resources that can potentially take a long time to load. I'd rather wait for elements on the DOM to be present than wait for everything to load. Some of my tests seem to take twice as long in WebDriver because of this.

推荐答案

是和否.从 Selenium 2.24.1 开始,对此的支持仅在 Firefox 中 - 您必须运行它在特殊的模式":

Yes and no. As of Selenium 2.24.1, the support for this is only in Firefox - you have to run it in a special "mode":

FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("webdriver.load.strategy", "unstable");
WebDriver driver = new FirefoxDriver(fp);

您甚至可以设置 如果你想超时.此方法在 Firefox 以外的任何浏览器中都失败,并且在没有不稳定策略的情况下在 Firefox 中什么也不做:

You can even set the timeout if you want to. This method fails in any browser other than Firefox and does nothing in Firefox without the unstable strategy:

driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);

这篇关于Selenium WebDriver 无需等待页面加载即可进入页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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